Skip to content

Commit

Permalink
fix(manager/bazel-module): skip non-local .bazelrc imports (#23383)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-thm committed Jul 17, 2023
1 parent e75ff54 commit 84e1731
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
Expand Up @@ -9,3 +9,6 @@ build --jobs 600

# This file does not exist.
try-import %workspace%/local.bazelrc

# This file does not exist/is outside of the basePath
try-import /does-not-exist/.bazelrc
41 changes: 41 additions & 0 deletions lib/modules/manager/bazel-module/bazelrc.spec.ts
Expand Up @@ -16,6 +16,12 @@ function mockReadLocalFile(files: Record<string, string | null>) {
});
}

function mockIsValidLocalPath(files: Record<string, boolean>) {
fs.isValidLocalPath.mockImplementation((file: string): boolean => {
return file in files;
});
}

describe('modules/manager/bazel-module/bazelrc', () => {
describe('BazelOption', () => {
it.each`
Expand Down Expand Up @@ -88,6 +94,7 @@ describe('modules/manager/bazel-module/bazelrc', () => {
describe('read()', () => {
it('when .bazelrc does not exist', async () => {
mockReadLocalFile({ '.bazelrc': null });
mockIsValidLocalPath({ '.bazelrc': true });
const result = await read('.');
expect(result).toHaveLength(0);
});
Expand All @@ -99,6 +106,7 @@ describe('modules/manager/bazel-module/bazelrc', () => {
build --show_timestamps --keep_going --jobs 600
`,
});
mockIsValidLocalPath({ '.bazelrc': true });
const result = await read('.');
expect(result).toEqual([
new CommandEntry('build', [
Expand All @@ -117,6 +125,7 @@ describe('modules/manager/bazel-module/bazelrc', () => {
build --color=yes
`,
});
mockIsValidLocalPath({ '.bazelrc': true });
const result = await read('.');
expect(result).toEqual([
new CommandEntry('build', [
Expand All @@ -141,6 +150,11 @@ describe('modules/manager/bazel-module/bazelrc', () => {
build --color=yes
`,
});
mockIsValidLocalPath({
'.bazelrc': true,
'local.bazelrc': true,
'shared.bazelrc': true,
});
const result = await read('.');
expect(result).toEqual([
new CommandEntry('build', [new BazelOption('show_timestamps')]),
Expand All @@ -156,6 +170,7 @@ describe('modules/manager/bazel-module/bazelrc', () => {
`,
'local.bazelrc': null,
});
mockIsValidLocalPath({ '.bazelrc': true });
const result = await read('.');
expect(result).toEqual([
new CommandEntry('build', [new BazelOption('jobs', '600')]),
Expand All @@ -175,6 +190,11 @@ describe('modules/manager/bazel-module/bazelrc', () => {
build --show_timestamps
`,
});
mockIsValidLocalPath({
'.bazelrc': true,
'foo.bazelrc': true,
'shared.bazelrc': true,
});
const result = await read('.');
expect(result).toEqual([
new CommandEntry('build', [new BazelOption('show_timestamps')]),
Expand All @@ -195,12 +215,33 @@ describe('modules/manager/bazel-module/bazelrc', () => {
import %workspace%/shared.bazelrc
`,
});
mockIsValidLocalPath({
'.bazelrc': true,
'foo.bazelrc': true,
'shared.bazelrc': true,
});
expect.assertions(1);
await expect(read('.')).rejects.toEqual(
new Error(
'Attempted to read a bazelrc multiple times. file: shared.bazelrc'
)
);
});

it('when .bazelrc refers to a non-local file', async () => {
mockReadLocalFile({
'.bazelrc': codeBlock`
import /non-local.bazelrc
build --jobs 600
`,
});
mockIsValidLocalPath({
'.bazelrc': true,
});
const result = await read('.');
expect(result).toEqual([
new CommandEntry('build', [new BazelOption('jobs', '600')]),
]);
});
});
});
9 changes: 7 additions & 2 deletions lib/modules/manager/bazel-module/bazelrc.ts
@@ -1,4 +1,5 @@
import upath from 'upath';
import { logger } from '../../../logger';
import { isNotNullOrUndefined } from '../../../util/array';
import * as fs from '../../../util/fs';
import { regEx } from '../../../util/regex';
Expand Down Expand Up @@ -124,8 +125,12 @@ async function readFile(
const importFile = upath.normalize(
entry.path.replace('%workspace%', workspaceDir)
);
const importEntries = await readFile(importFile, workspaceDir, readFiles);
results.push(...importEntries);
if (fs.isValidLocalPath(importFile)) {
const importEntries = await readFile(importFile, workspaceDir, readFiles);
results.push(...importEntries);
} else {
logger.debug(`Skipping non-local .bazelrc import ${importFile}`);
}
}
return results;
}
Expand Down

0 comments on commit 84e1731

Please sign in to comment.