Skip to content

Commit

Permalink
Fix getting package references from csproj from multiple item groups (#…
Browse files Browse the repository at this point in the history
…3603)

* Fix getting package references from csproj from multiple item groups

* Add comment
  • Loading branch information
alexweininger authored Mar 2, 2023
1 parent c17edf5 commit 9e27857
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
28 changes: 28 additions & 0 deletions src/utils/dotnetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,32 @@ export namespace dotnetUtils {
}
return result;
}

interface CSProjXml {
Project?: {
ItemGroup?: {
PackageReference?: {
'$': CSProjPackageReference;
}[];
}[];
}
}

type CSProjPackageReference = {
Include: string;
}

export function getPackageReferences(xml: CSProjXml): CSProjPackageReference[] {
const packageReferences: CSProjPackageReference[] = [];
// package references can be split across multiple item groups
xml.Project?.ItemGroup?.forEach((itemGroup) => {
const references = itemGroup.PackageReference;
if (references) {
references.forEach((reference) => {
packageReferences.push(reference.$);
});
}
});
return packageReferences;
}
}
10 changes: 3 additions & 7 deletions src/utils/durableUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ext } from "../extensionVariables";
import { IHostJsonV2, INetheriteTaskJson, ISqlTaskJson, IStorageTaskJson } from "../funcConfig/host";
import { localize } from "../localize";
import { cpUtils } from "./cpUtils";
import { dotnetUtils } from "./dotnetUtils";
import { hasNodeJsDependency } from "./nodeJsUtils";
import { pythonUtils } from "./pythonUtils";
import { venvUtils } from "./venvUtils";
Expand Down Expand Up @@ -107,14 +108,9 @@ export namespace durableUtils {
const csProjContents: string = await AzExtFsExtra.readFile(csProjPaths[0].path);

return new Promise((resolve) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
xml2js.parseString(csProjContents, { explicitArray: false }, (err: any, result: any): void => {
xml2js.parseString(csProjContents, (err: Error, result: unknown): void => {
if (result && !err) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
let packageReferences = result?.['Project']?.['ItemGroup']?.[0]?.PackageReference ?? [];
packageReferences = (packageReferences instanceof Array) ? packageReferences : [packageReferences];
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
resolve(packageReferences.some(p => /Durable/i.test(p?.['$']?.['Include'] ?? '')));
resolve(dotnetUtils.getPackageReferences(result).some(p => /Durable/i.test(p.Include)));
}
});
});
Expand Down

0 comments on commit 9e27857

Please sign in to comment.