Skip to content

Commit

Permalink
feat(core): include non-project files on fileMap and filesToProcess (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Sep 20, 2023
1 parent 6ea3d38 commit f213976
Show file tree
Hide file tree
Showing 37 changed files with 888 additions and 415 deletions.
8 changes: 4 additions & 4 deletions docs/generated/devkit/CreateDependenciesContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Context for [CreateDependencies](../../devkit/documents/CreateDependencies)
### Properties

- [externalNodes](../../devkit/documents/CreateDependenciesContext#externalnodes): Record<string, ProjectGraphExternalNode>
- [fileMap](../../devkit/documents/CreateDependenciesContext#filemap): ProjectFileMap
- [filesToProcess](../../devkit/documents/CreateDependenciesContext#filestoprocess): ProjectFileMap
- [fileMap](../../devkit/documents/CreateDependenciesContext#filemap): FileMap
- [filesToProcess](../../devkit/documents/CreateDependenciesContext#filestoprocess): FileMap
- [nxJsonConfiguration](../../devkit/documents/CreateDependenciesContext#nxjsonconfiguration): NxJsonConfiguration<string[] | "\*">
- [projects](../../devkit/documents/CreateDependenciesContext#projects): Record<string, ProjectConfiguration>
- [workspaceRoot](../../devkit/documents/CreateDependenciesContext#workspaceroot): string
Expand All @@ -25,15 +25,15 @@ The external nodes that have been added to the graph.

### fileMap

`Readonly` **fileMap**: [`ProjectFileMap`](../../devkit/documents/ProjectFileMap)
`Readonly` **fileMap**: `FileMap`

All files in the workspace

---

### filesToProcess

`Readonly` **filesToProcess**: [`ProjectFileMap`](../../devkit/documents/ProjectFileMap)
`Readonly` **filesToProcess**: `FileMap`

Files changes since last invocation

Expand Down
10 changes: 8 additions & 2 deletions docs/generated/devkit/FileData.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ Some metadata about a file

### Properties

- [deps](../../devkit/documents/FileData#deps): (string | [string, string])[]
- [deps](../../devkit/documents/FileData#deps): FileDataDependency[]
- [file](../../devkit/documents/FileData#file): string
- [hash](../../devkit/documents/FileData#hash): string

## Properties

### deps

`Optional` **deps**: (`string` \| [`string`, `string`])[]
`Optional` **deps**: `FileDataDependency`[]

An array of dependencies. If an element is just a string,
the dependency is assumed to be a static dependency targetting
that string. If the element is a tuple with two elements, the first element
inside of it is the target project, with the second element being the type of dependency.
If the tuple has 3 elements, the first is preceded by a source.

---

Expand Down
28 changes: 18 additions & 10 deletions docs/generated/devkit/ProjectGraphBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ The ProjectGraphProcessor has been deprecated. Use a [CreateNodes](../../devkit/

### Properties

- [fileMap](../../devkit/documents/ProjectGraphBuilder#filemap): ProjectFileMap
- [graph](../../devkit/documents/ProjectGraphBuilder#graph): ProjectGraph
- [nonProjectFiles](../../devkit/documents/ProjectGraphBuilder#nonprojectfiles): FileData[]
- [projectFileMap](../../devkit/documents/ProjectGraphBuilder#projectfilemap): ProjectFileMap
- [removedEdges](../../devkit/documents/ProjectGraphBuilder#removededges): Object

### Methods
Expand All @@ -40,26 +41,33 @@ The ProjectGraphProcessor has been deprecated. Use a [CreateNodes](../../devkit/

### constructor

**new ProjectGraphBuilder**(`graph?`, `fileMap?`)
**new ProjectGraphBuilder**(`graph?`, `projectFileMap?`, `nonProjectFiles?`)

#### Parameters

| Name | Type |
| :--------- | :-------------------------------------------------------- |
| `graph?` | [`ProjectGraph`](../../devkit/documents/ProjectGraph) |
| `fileMap?` | [`ProjectFileMap`](../../devkit/documents/ProjectFileMap) |
| Name | Type |
| :----------------- | :-------------------------------------------------------- |
| `graph?` | [`ProjectGraph`](../../devkit/documents/ProjectGraph) |
| `projectFileMap?` | [`ProjectFileMap`](../../devkit/documents/ProjectFileMap) |
| `nonProjectFiles?` | [`FileData`](../../devkit/documents/FileData)[] |

## Properties

### fileMap
### graph

`Private` `Readonly` **fileMap**: [`ProjectFileMap`](../../devkit/documents/ProjectFileMap)
`Readonly` **graph**: [`ProjectGraph`](../../devkit/documents/ProjectGraph)

---

### graph
### nonProjectFiles

`Readonly` **graph**: [`ProjectGraph`](../../devkit/documents/ProjectGraph)
`Private` `Readonly` **nonProjectFiles**: [`FileData`](../../devkit/documents/FileData)[]

---

### projectFileMap

`Private` `Readonly` **projectFileMap**: [`ProjectFileMap`](../../devkit/documents/ProjectFileMap)

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { ImportDeclaration, ImportSpecifier, Node } from 'typescript';
import { FileChangeRecorder } from '../../utils/file-change-recorder';
import { ngrxVersion } from '../../utils/versions';
import { getProjectsFilteredByDependencies } from '../utils/projects';
import { readProjectFileMapCache } from 'nx/src/project-graph/nx-deps-cache';
import { readFileMapCache } from 'nx/src/project-graph/nx-deps-cache';
import { fileDataDepTarget } from 'nx/src/config/project-graph';

let tsquery: typeof import('@phenomnomnominal/tsquery').tsquery;
Expand All @@ -35,7 +35,7 @@ export default async function (tree: Tree): Promise<void> {

ensureTypescript();
tsquery = require('@phenomnomnominal/tsquery').tsquery;
const cachedFileMap = readProjectFileMapCache().projectFileMap;
const cachedFileMap = readFileMapCache().fileMap.projectFileMap;

const filesWithNxAngularImports: FileData[] = [];
for (const { graphNode } of projects) {
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/dependency-checks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ProjectGraphExternalNode,
} from '@nx/devkit';
import { Linter } from 'eslint';
import { FileDataDependency } from 'nx/src/config/project-graph';

jest.mock('@nx/devkit', () => ({
...jest.requireActual<any>('@nx/devkit'),
Expand Down Expand Up @@ -1652,7 +1653,7 @@ it('should require swc if @nx/js:swc executor', () => {
expect(failures[0].line).toEqual(3);
});

function createFile(f: string, deps?: (string | [string, string])[]): FileData {
function createFile(f: string, deps?: FileDataDependency[]): FileData {
return { file: f, hash: '', deps };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import enforceModuleBoundaries, {
RULE_NAME as enforceModuleBoundariesRuleName,
} from '../../src/rules/enforce-module-boundaries';
import { createProjectRootMappings } from 'nx/src/project-graph/utils/find-project-for-path';
import { FileDataDependency } from 'nx/src/config/project-graph';

jest.mock('@nx/devkit', () => ({
...jest.requireActual<any>('@nx/devkit'),
Expand Down Expand Up @@ -1303,8 +1304,8 @@ Violation detected in:
{
mylibName: [
createFile(`libs/mylib/src/main.ts`, [
['otherName', 'static'],
['otherName', 'dynamic'],
['otherName', DependencyType.static],
['otherName', DependencyType.dynamic],
]),
],
otherName: [createFile(`libs/other/index.ts`)],
Expand Down Expand Up @@ -2250,7 +2251,7 @@ const baseConfig = {
linter.defineParser('@typescript-eslint/parser', parser);
linter.defineRule(enforceModuleBoundariesRuleName, enforceModuleBoundaries);

function createFile(f: string, deps?: (string | [string, string])[]): FileData {
function createFile(f: string, deps?: FileDataDependency[]): FileData {
return { file: f, hash: '', deps };
}

Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/utils/graph-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export function findFilesInCircularPath(

for (let i = 0; i < circularPath.length - 1; i++) {
const next = circularPath[i + 1].name;
const files: FileData[] = projectFileMap[circularPath[i].name] || [];
const files = projectFileMap[circularPath[i].name] || [];
filePathChain.push(
files
.filter(
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/utils/project-graph-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'nx/src/project-graph/utils/find-project-for-path';
import { readNxJson } from 'nx/src/config/configuration';
import { TargetProjectLocator } from '@nx/js/src/internal';
import { readProjectFileMapCache } from 'nx/src/project-graph/nx-deps-cache';
import { readFileMapCache } from 'nx/src/project-graph/nx-deps-cache';

export function ensureGlobalProjectGraph(ruleName: string) {
/**
Expand All @@ -37,7 +37,7 @@ export function ensureGlobalProjectGraph(ruleName: string) {
globalThis.projectRootMappings = createProjectRootMappings(
projectGraph.nodes
);
globalThis.projectFileMap = readProjectFileMapCache().projectFileMap;
globalThis.projectFileMap = readFileMapCache().fileMap.projectFileMap;
globalThis.targetProjectLocator = new TargetProjectLocator(
projectGraph.nodes,
projectGraph.externalNodes
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/utils/package-json/update-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { writeFileSync } from 'fs-extra';
import { fileExists } from 'nx/src/utils/fileutils';
import type { PackageJson } from 'nx/src/utils/package-json';
import { existsSync } from 'fs';
import { readProjectFileMapCache } from 'nx/src/project-graph/nx-deps-cache';
import { readFileMapCache } from 'nx/src/project-graph/nx-deps-cache';

import { getRelativeDirectoryToProjectRoot } from '../get-main-file-dir';

Expand Down Expand Up @@ -57,7 +57,7 @@ export function updatePackageJson(
): void {
let packageJson: PackageJson;
if (fileMap == null) {
fileMap = readProjectFileMapCache()?.projectFileMap || {};
fileMap = readFileMapCache()?.fileMap?.projectFileMap || {};
}

if (options.updateBuildableProjectDepsInPackageJson) {
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import { TaskGraph } from '../../config/task-graph';
import { daemonClient } from '../../daemon/client/client';
import { Server } from 'net';
import { readProjectFileMapCache } from '../../project-graph/nx-deps-cache';
import { readFileMapCache } from '../../project-graph/nx-deps-cache';
import { getAffectedGraphNodes } from '../affected/affected';
import { splitArgsIntoNxArgsAndOverrides } from '../../utils/command-line-utils';

Expand Down Expand Up @@ -576,7 +576,7 @@ async function createDepGraphClientResponse(
let graph = pruneExternalNodes(
await createProjectGraphAsync({ exitOnError: true })
);
let fileMap = readProjectFileMapCache().projectFileMap;
let fileMap = readFileMapCache().fileMap.projectFileMap;
performance.mark('project graph watch calculation:end');
performance.mark('project graph response generation:start');

Expand Down
42 changes: 37 additions & 5 deletions packages/nx/src/config/project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,47 @@ import { NxJsonConfiguration } from './nx-json';
export interface FileData {
file: string;
hash: string;
deps?: (string | [string, string])[];
/**
* An array of dependencies. If an element is just a string,
* the dependency is assumed to be a static dependency targetting
* that string. If the element is a tuple with two elements, the first element
* inside of it is the target project, with the second element being the type of dependency.
* If the tuple has 3 elements, the first is preceded by a source.
*/
deps?: FileDataDependency[];
}

/**
* A file data dependency, as stored in the cache. If just a string,
* the dependency is assumed to be a static dependency targetting
* that string. If it is a tuple with two elements, the first element
* inside of it is the target project, with the second element being the type of dependency.
* If the tuple has 3 elements, the first is preceded by a source.
*/
export type FileDataDependency =
| string
| [target: string, type: DependencyType]
| [source: string, target: string, type: DependencyType];

export function fileDataDepTarget(dep: FileDataDependency) {
return typeof dep === 'string'
? dep
: Array.isArray(dep) && dep.length === 2
? dep[0]
: dep[1];
}

export function fileDataDepTarget(dep: string | [string, string]) {
return typeof dep === 'string' ? dep : dep[0];
export function fileDataDepType(dep: FileDataDependency) {
return typeof dep === 'string'
? 'static'
: Array.isArray(dep) && dep.length === 2
? dep[1]
: dep[2];
}

export function fileDataDepType(dep: string | [string, string]) {
return typeof dep === 'string' ? 'static' : dep[1];
export interface FileMap {
nonProjectFiles: FileData[];
projectFileMap: ProjectFileMap;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { performance } from 'perf_hooks';
import { projectFileMapWithFiles } from '../project-graph-incremental-recomputation';
import { fileMapWithFiles } from '../project-graph-incremental-recomputation';

export type ChangedFile = {
path: string;
Expand Down Expand Up @@ -38,7 +38,7 @@ export function getProjectsAndGlobalChanges(

const fileToProjectMap: Record<string, string> = {};
for (const [projectName, projectFiles] of Object.entries(
projectFileMapWithFiles?.projectFileMap ?? {}
fileMapWithFiles?.fileMap?.projectFileMap ?? {}
)) {
for (const projectFile of projectFiles) {
fileToProjectMap[projectFile.file] = projectName;
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/daemon/server/handle-hash-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export async function handleHashTasks(payload: {
}) {
setHashEnv(payload.env);

const { projectGraph, allWorkspaceFiles, projectFileMap } =
const { projectGraph, allWorkspaceFiles, fileMap } =
await getCachedSerializedProjectGraphPromise();
const nxJson = readNxJson();

if (projectGraph !== storedProjectGraph) {
storedProjectGraph = projectGraph;
storedHasher = new InProcessTaskHasher(
projectFileMap,
fileMap?.projectFileMap,
allWorkspaceFiles,
projectGraph,
nxJson,
Expand Down
Loading

0 comments on commit f213976

Please sign in to comment.