diff --git a/libs/core/src/true-affected.ts b/libs/core/src/true-affected.ts index 21367cb..c39d363 100644 --- a/libs/core/src/true-affected.ts +++ b/libs/core/src/true-affected.ts @@ -33,9 +33,9 @@ export const trueAffected = async ({ ...(rootTsConfig == null ? {} : { - tsConfigFilePath: resolve(cwd, rootTsConfig), - skipAddingFilesFromTsConfig: true, - }), + tsConfigFilePath: resolve(cwd, rootTsConfig), + skipAddingFilesFromTsConfig: true, + }), }); const implicitDeps = ( @@ -71,16 +71,25 @@ export const trueAffected = async ({ ({ filePath }) => project.getSourceFile(resolve(cwd, filePath)) != null ); + // These files declare dependencies or tasks (Nx targets) of a project and should render the project affected if changed + const depAndTaskDeclarationFiles = ['package.json', 'nx.json', 'project.json'] + const ignoredPaths = ['./node_modules', './dist', './.git']; + const affectedPackages = new Set(); + const nonSourceChangedFiles: GetChangedFiles[] = changedFiles .filter( - ({ filePath }) => - !filePath.match(/.*\.(ts|js)x?$/g) && - project.getSourceFile(resolve(cwd, filePath)) == null + function({ filePath }) { + if (depAndTaskDeclarationFiles.includes(filePath.substring(filePath.lastIndexOf('/') + 1))) { + const pkg = getPackageNameByPath(resolve(cwd, filePath), projects, true); + if (pkg) affectedPackages.add(pkg); + } + return !filePath.match(/.*\.(ts|js)x?$/g) && + project.getSourceFile(resolve(cwd, filePath)) == null + } ) - .flatMap(({ filePath: changedFilePath }) => - findNonSourceAffectedFiles(cwd, changedFilePath, ignoredPaths) + .flatMap(({ filePath: changedFilePath }) => findNonSourceAffectedFiles(cwd, changedFilePath, ignoredPaths) ); const filteredChangedFiles = [ @@ -99,7 +108,8 @@ export const trueAffected = async ({ .map(({ filePath }) => getPackageNameByPath(filePath, projects)) .filter((v): v is string => v != null); - const affectedPackages = new Set(changedIncludedFilesPackages); + changedIncludedFilesPackages.forEach(affectedPackages.add) + const visitedIdentifiers = new Map(); const findReferencesLibs = (node: Node) => { diff --git a/libs/core/src/utils.ts b/libs/core/src/utils.ts index 04c3ea3..a909b7f 100644 --- a/libs/core/src/utils.ts +++ b/libs/core/src/utils.ts @@ -16,9 +16,20 @@ export const findRootNode = ( export const getPackageNameByPath = ( path: string, - projects: TrueAffectedProject[] + projects: TrueAffectedProject[], + // Search files in the project root as well + includesRoot = false, ): string | undefined => { - return projects.find(({ sourceRoot }) => path.includes(sourceRoot))?.name; + return projects.map(({ name, sourceRoot }) => ({ + name, + root: includesRoot ? sourceRoot.substring(0, sourceRoot.lastIndexOf("/")) : sourceRoot + })) + // In case of nested project paths (for example when there's a root project.json): + // sort the paths from the longest to the shortest so the sub-directories come before their parent directories + .sort((a, b) => b.root.length - a.root.length) + .find( + ({ root }) => path.includes(root) + )?.name; }; export function findNonSourceAffectedFiles(