Skip to content

Commit

Permalink
fix(core): prevent stale cache entries from breaking swagger on dotne…
Browse files Browse the repository at this point in the history
…t upgrades (#633)
  • Loading branch information
AgentEnder authored Feb 27, 2023
1 parent e6128d0 commit 71f6893
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 19 deletions.
2 changes: 1 addition & 1 deletion demo/apps/webapi/NxDotnet.Test.Webapi.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
6 changes: 3 additions & 3 deletions demo/apps/webapi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
// Learn more about configuring Swagger at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
{
app.UseSwagger();
app.UseSwaggerUI();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
6 changes: 0 additions & 6 deletions global.json

This file was deleted.

55 changes: 53 additions & 2 deletions packages/core/src/executors/build/executor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { ExecutorContext, workspaceRoot } from '@nrwl/devkit';
import {
ExecutorContext,
ProjectConfiguration,
workspaceRoot,
} from '@nrwl/devkit';

import { resolve } from 'path';
import { rmSync, statSync } from 'fs';
import { join, resolve } from 'path';

import { DotNetClient, dotnetFactory } from '@nx-dotnet/dotnet';
import {
getExecutedProjectConfiguration,
getProjectFileForNxProject,
inlineNxTokens,
} from '@nx-dotnet/utils';

import { BuildExecutorSchema } from './schema';
Expand All @@ -16,6 +22,8 @@ export default async function runExecutor(
dotnetClient: DotNetClient = new DotNetClient(dotnetFactory()),
) {
const nxProjectConfiguration = getExecutedProjectConfiguration(context);
removeOldArtifacts(context, nxProjectConfiguration);

dotnetClient.cwd = resolve(workspaceRoot, nxProjectConfiguration.root);
dotnetClient.printSdkVersion();
const projectFilePath = resolve(
Expand All @@ -35,3 +43,46 @@ export default async function runExecutor(
success: true,
};
}

function removeOldArtifacts(
context: ExecutorContext,
projectConfiguration: ProjectConfiguration,
) {
const outputs = context.target?.outputs?.map((output) =>
join(context.root, inlineNxTokens(output, projectConfiguration)),
);
if (
!outputs &&
Object.values(context.nxJsonConfiguration?.tasksRunnerOptions ?? {}).some(
(runnerOptions) =>
runnerOptions.options?.cacheableOperations?.includes(
context.targetName,
),
)
) {
throw new Error(`[nx-dotnet] ${context.projectGraph}:${context.targetName} is cacheable, but has no outputs listed.
This will result in cache hits not retrieving build artifacts, only terminal outputs.
See: https://nx.dev/reference/project-configuration#outputs`);
}
for (const output of outputs || []) {
if (
// No reason to clear build intermediates, just makes the resulting build command slower.
!output.includes('intermediates') &&
!output.endsWith('obj') &&
// Prevent exceptions from trying to rmdirSync(globPattern)
getStatsOrNull(output)?.isDirectory()
) {
rmSync(output, { recursive: true });
}
}
}

function getStatsOrNull(f: string) {
try {
return statSync(f);
} catch {
return null;
}
}
17 changes: 11 additions & 6 deletions packages/core/src/generators/utils/get-path-to-startup-assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,24 @@ export function buildStartupAssemblyPath(
/(?:\.csproj|\.vbproj|\.fsproj)$/,
'.dll',
);
const foundDll = sync(`**/${dllName}`, { cwd: outputDirectory })[0];
if (!foundDll) {
const matchingDlls = sync(`**/${dllName}`, { cwd: outputDirectory });
if (!matchingDlls.length) {
throw new Error(
`[nx-dotnet] Unable to locate ${dllName} in ${relative(
workspaceRoot,
outputDirectory,
)}`,
);
}
return joinPathFragments(
outputDirectory,
sync(`**/${dllName}`, { cwd: outputDirectory })[0],
);
if (matchingDlls.length > 1) {
throw new Error(
`[nx-dotnet] Located multiple matching dlls for ${projectName}.
You may need to clean old build artifacts from your outputs, or manually
specify the path to the output assembly within ${project.root}/project.json.`,
);
}
return joinPathFragments(outputDirectory, matchingDlls[0]);
}

function findBuildTarget(
Expand Down
14 changes: 14 additions & 0 deletions packages/utils/src/lib/utility-functions/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@nrwl/devkit';

import { readFileSync } from 'fs';
import { NX_PREFIX } from 'nx/src/utils/logger';
import { dirname, isAbsolute, relative, resolve } from 'path';
import { XmlDocument, XmlElement } from 'xmldoc';

Expand Down Expand Up @@ -135,3 +136,16 @@ export function getProjectFilesForProject(
function normalizePath(p: string): string {
return nxNormalizePath(p).split('\\').join('/');
}

export function inlineNxTokens(value: string, project: ProjectConfiguration) {
if (value.startsWith('{workspaceRoot}/')) {
value = value.replace(/^\{workspaceRoot\}\//, '');
}
if (value.includes('{workspaceRoot}')) {
throw new Error(
`${NX_PREFIX} The {workspaceRoot} token is only valid at the beginning of an output.`,
);
}
value = value.replace('{projectRoot}', project.root);
return value.replace('{projectName}', project.name as string);
}

0 comments on commit 71f6893

Please sign in to comment.