diff --git a/libs/core/src/true-affected.ts b/libs/core/src/true-affected.ts index 08cc111..27f28c6 100644 --- a/libs/core/src/true-affected.ts +++ b/libs/core/src/true-affected.ts @@ -35,9 +35,9 @@ export const trueAffected = async ({ ...(rootTsConfig == null ? {} : { - tsConfigFilePath: resolve(cwd, rootTsConfig), - skipAddingFilesFromTsConfig: true, - }), + tsConfigFilePath: resolve(cwd, rootTsConfig), + skipAddingFilesFromTsConfig: true, + }), }); const implicitDeps = ( @@ -73,17 +73,26 @@ 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 = changedFiles .filter( - ({ filePath }) => - !filePath.match(/.*\.(ts|js)x?$/g) && - !filePath.endsWith(lockFileName) && - 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) && + !filePath.endsWith(lockFileName) && + project.getSourceFile(resolve(cwd, filePath)) == null + } ) - .flatMap(({ filePath: changedFilePath }) => - findNonSourceAffectedFiles(cwd, changedFilePath, ignoredPaths) + .flatMap(({ filePath: changedFilePath }) => findNonSourceAffectedFiles(cwd, changedFilePath, ignoredPaths) ); let changedFilesByLockfile: ChangedFiles[] = []; @@ -114,7 +123,8 @@ export const trueAffected = async ({ .map(({ filePath }) => getPackageNameByPath(filePath, projects)) .filter((v): v is string => v != null); - const affectedPackages = new Set(changedIncludedFilesPackages); + changedIncludedFilesPackages.forEach(pkg => affectedPackages.add(pkg)) + const visitedIdentifiers = new Map(); const findReferencesLibs = (node: Node) => { diff --git a/libs/core/src/utils.ts b/libs/core/src/utils.ts index 3a201c7..8403818 100644 --- a/libs/core/src/utils.ts +++ b/libs/core/src/utils.ts @@ -12,7 +12,18 @@ 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; };