Skip to content
Snippets Groups Projects
Unverified Commit 13027fd4 authored by Michael Kriese's avatar Michael Kriese Committed by GitHub
Browse files

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


* fix(manager/nuget): optimize xml error handling

* Update lib/modules/manager/nuget/package-tree.spec.ts

Co-authored-by: default avatarSergei Zharinov <zharinov@users.noreply.github.com>

Co-authored-by: default avatarSergei Zharinov <zharinov@users.noreply.github.com>
parent c95024bb
No related branches found
No related tags found
No related merge requests found
......@@ -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'
);
});
});
});
......@@ -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';
......@@ -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'))
......
......@@ -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;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment