Skip to content

Commit

Permalink
fix: fixing bug where .deps file was placed in a subdir of the publis…
Browse files Browse the repository at this point in the history
…h folder (#205)
  • Loading branch information
dotkas authored Apr 26, 2024
1 parent a016522 commit 5023d53
Show file tree
Hide file tree
Showing 7 changed files with 4,114 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/nuget-parser/cli/dotnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export async function publish(
const tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), `snyk-nuget-plugin-publish-csharp-`),
);

// See https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/7.0/solution-level-output-no-longer-valid#recommended-action
// about why we're not using `--output` for this.
args.push(`--property:PublishDir=${tempDir}`);
Expand Down
45 changes: 40 additions & 5 deletions lib/nuget-parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import * as dotnetCoreV2Parser from './parsers/dotnet-core-v2-parser';
import * as dotnetFrameworkParser from './parsers/dotnet-framework-parser';
import * as projectJsonParser from './parsers/project-json-parser';
import * as packagesConfigParser from './parsers/packages-config-parser';
import { FileNotProcessableError, InvalidManifestError } from '../errors';
import {
CliCommandError,
FileNotProcessableError,
InvalidManifestError,
} from '../errors';
import {
AssemblyVersions,
DotnetCoreV2Results,
Expand Down Expand Up @@ -71,6 +75,34 @@ function getFileContents(fileContentPath: string): string {
}
}

// `dotnet` can publish the .deps file to a variety of places inside the publish folder, depending on what you're
// including and targeting. Instead of trying different directories, just scan them all. In most cases, the file
// will be in the root directory. (See https://github.com/Azure/azure-functions-vs-build-sdk/issues/518)
function findDepsFilePathInPublishDir(dir: string): string | null {
for (const item of fs.readdirSync(dir)) {
const itemPath = path.join(dir, item);

// The file is usually <project>.deps.json, but in edge cases, `dotnet` names it for you.
if (itemPath.endsWith('deps.json')) {
return itemPath;
}

if (!fs.statSync(itemPath).isDirectory()) {
continue;
}

// Otherwise, look in a nested dir for the same thing.
const foundFilePath = findDepsFilePathInPublishDir(itemPath);
if (!foundFilePath) {
continue;
}

return foundFilePath;
}

return null;
}

export async function buildDepGraphFromFiles(
root: string | undefined,
targetFile: string | undefined,
Expand Down Expand Up @@ -162,10 +194,13 @@ Will attempt to build dependency graph anyway, but the operation might fail.`);
const publishDir = await dotnet.publish(safeRoot, decidedTargetFramework);

// Then inspect the dependency graph for the runtimepackage's assembly versions.
const depsFilePath = path.resolve(
publishDir,
`${projectNameFromManifestFile}.deps.json`,
);
const filename = `${projectNameFromManifestFile}.deps.json`;
const depsFilePath = findDepsFilePathInPublishDir(publishDir);
if (!depsFilePath) {
throw new CliCommandError(
`unable to locate ${filename} anywhere inside ${publishDir}, file is needed for runtime resolution to occur, aborting`,
);
}

const depsFile = fs.readFileSync(depsFilePath);
const publishedProjectDeps: PublishedProjectDeps = JSON.parse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# dotnet_8_functions_bin_folder

Including a `<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3"/>` above version 4.x will cause
a `dotnet publish` to add `/bin` to the publish dir, confusing this logic on where the `.deps` file, containing runtime
pack information, is located.

See [this thread for more](https://github.com/Azure/azure-functions-vs-build-sdk/issues/518).
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.3.0" />
</ItemGroup>
</Project>
Loading

0 comments on commit 5023d53

Please sign in to comment.