Skip to content

Improve paths config handling for JS/Python when globs are used #2061

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/analysis-paths.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/analysis-paths.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 32 additions & 2 deletions lib/analysis-paths.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/analysis-paths.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 36 additions & 2 deletions src/analysis-paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,45 @@ test("nonEmptyPaths", async (t) => {
trapCacheDownloadTime: 0,
};
analysisPaths.includeAndExcludeAnalysisPaths(config);
t.is(process.env["LGTM_INDEX_INCLUDE"], "path1\npath2");
t.is(process.env["LGTM_INDEX_INCLUDE"], undefined);
t.is(process.env["LGTM_INDEX_EXCLUDE"], "path4\npath5");
t.is(
process.env["LGTM_INDEX_FILTERS"],
"include:path1\ninclude:path2\ninclude:**/path3\nexclude:path4\nexclude:path5\nexclude:path6/**",
"exclude:**/*\ninclude:path1\ninclude:path2\ninclude:**/path3\nexclude:path4\nexclude:path5\nexclude:path6/**",
);
});
});

test("paths only has globs", async (t) => {
return await util.withTmpDir(async (tmpDir) => {
const config = {
languages: [],
queries: {},
paths: ["path1/**", "**/path2"],
pathsIgnore: [],
originalUserInput: {},
tempDir: tmpDir,
codeQLCmd: "",
gitHubVersion: { type: util.GitHubVariant.DOTCOM } as util.GitHubVersion,
dbLocation: path.resolve(tmpDir, "codeql_databases"),
packs: {},
debugMode: false,
debugArtifactName: util.DEFAULT_DEBUG_ARTIFACT_NAME,
debugDatabaseName: util.DEFAULT_DEBUG_DATABASE_NAME,
augmentationProperties: {
injectedMlQueries: false,
packsInputCombines: false,
queriesInputCombines: false,
},
trapCaches: {},
trapCacheDownloadTime: 0,
};
analysisPaths.includeAndExcludeAnalysisPaths(config);
t.is(process.env["LGTM_INDEX_INCLUDE"], undefined);
t.is(process.env["LGTM_INDEX_EXCLUDE"], undefined);
t.is(
process.env["LGTM_INDEX_FILTERS"],
"exclude:**/*\ninclude:path1/**\ninclude:**/path2",
);
});
});
Expand Down
16 changes: 14 additions & 2 deletions src/analysis-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ export function includeAndExcludeAnalysisPaths(config: configUtils.Config) {
// excluding and not traversing entire file subtrees.
// It does not understand globs or double-globs because that would require it to
// traverse the entire file tree to determine which files are matched.
// Any paths containing "*" are not included in these.
if (config.paths.length !== 0) {
//
// Since globs aren't handled by LGTM_INDEX_INCLUDE, if any of files/directories
// include paths contains a glob or double-glob, we fall back to not setting
// LGTM_INDEX_INCLUDE at all, and only use the LGTM_INDEX_FILTERS mechanism to control
// which files are extracted.
const pathsHasGlob = config.paths.some((p) => p.indexOf("*") !== -1);
if (config.paths.length !== 0 && pathsHasGlob === false) {
process.env["LGTM_INDEX_INCLUDE"] = buildIncludeExcludeEnvVar(config.paths);
}
// If the temporary or tools directory is in the working directory ignore that too.
Expand All @@ -69,6 +74,13 @@ export function includeAndExcludeAnalysisPaths(config: configUtils.Config) {
// extracted or ignored. It does not control which directories are traversed.
// This does understand the glob and double-glob syntax.
const filters: string[] = [];
if (pathsHasGlob) {
// if paths have globs we don't set LGTM_INDEX_INCLUDE and therefore by default we
// would extract everything. By inserting `exclude:**/*` as the first element, we
// need to change the default to exclude everything, meaning only the things
// mentioned in paths will be included.
filters.push("exclude:**/*");
}
filters.push(...config.paths.map((p) => `include:${p}`));
filters.push(...config.pathsIgnore.map((p) => `exclude:${p}`));
if (filters.length !== 0) {
Expand Down