Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(manager/nuget): optimize xml error handling #16681

Merged
merged 3 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/modules/manager/nuget/package-tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,13 @@ describe('modules/manager/nuget/package-tree', () => {
'Circular reference detected in NuGet package files'
);
});

it('throws error on invalid xml file', async () => {
git.getFileList.mockResolvedValue(['foo/bar.csproj']);
Fixtures.mock({ '/tmp/repo/foo/bar.csproj': '<invalid' });
await expect(getDependentPackageFiles('foo/bar.csproj')).rejects.toThrow(
'Invalid xml file: foo/bar.csproj'
);
});
});
});
10 changes: 5 additions & 5 deletions lib/modules/manager/nuget/package-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import is from '@sindresorhus/is';
import Graph from 'graph-data-structure';
import minimatch from 'minimatch';
import upath from 'upath';
import xmldoc from 'xmldoc';
import { logger } from '../../../logger';
import { readLocalFile } from '../../../util/fs';
import { getFileList } from '../../../util/git';
import { readFileAsXmlDocument } from './util';

export const NUGET_CENTRAL_FILE = 'Directory.Packages.props';
export const MSBUILD_CENTRAL_FILE = 'Packages.props';
Expand Down Expand Up @@ -39,10 +38,11 @@ export async function getDependentPackageFiles(
}

for (const f of packageFiles) {
const packageFileContent = await readLocalFile(f, 'utf8');
const doc = await readFileAsXmlDocument(f);
if (!doc) {
throw new Error(`Invalid xml file: ${f}`);
}

// TODO #7154
const doc = new xmldoc.XmlDocument(packageFileContent!);
const projectReferenceAttributes = doc
.childrenNamed('ItemGroup')
.map((ig) => ig.childrenNamed('ProjectReference'))
Expand Down
8 changes: 5 additions & 3 deletions lib/modules/manager/nuget/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import { regEx } from '../../../util/regex';
import { defaultRegistryUrls } from '../../datasource/nuget';
import type { Registry } from './types';

async function readFileAsXmlDocument(
export async function readFileAsXmlDocument(
file: string
): Promise<XmlDocument | undefined> {
try {
// TODO #7154
return new XmlDocument((await readLocalFile(file, 'utf8'))!);
const doc = new XmlDocument((await readLocalFile(file, 'utf8'))!);
// don't return empty documents
return doc?.firstChild ? doc : undefined;
} catch (err) {
logger.debug({ err }, `failed to parse '${file}' as XML document`);
logger.debug({ err, file }, `failed to parse file as XML document`);
return undefined;
}
}
Expand Down