Skip to content
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

fix(js): ignore dependencies from non-index root files for create-package-json #17930

Merged
merged 4 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions docs/generated/devkit/nx_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ It only uses language primitives and immutable objects
- [NxAffectedConfig](../../devkit/documents/nx_devkit#nxaffectedconfig)
- [NxJsonConfiguration](../../devkit/documents/nx_devkit#nxjsonconfiguration)
- [NxPlugin](../../devkit/documents/nx_devkit#nxplugin)
- [PackageJson](../../devkit/documents/nx_devkit#packagejson)
- [ProjectConfiguration](../../devkit/documents/nx_devkit#projectconfiguration)
- [ProjectFileMap](../../devkit/documents/nx_devkit#projectfilemap)
- [ProjectGraph](../../devkit/documents/nx_devkit#projectgraph)
Expand Down Expand Up @@ -311,6 +312,14 @@ A plugin for Nx

---

### PackageJson
meeroslav marked this conversation as resolved.
Show resolved Hide resolved

• **PackageJson**: `Object`

Package Json configuration

---

### ProjectConfiguration

• **ProjectConfiguration**: `Object`
Expand Down
9 changes: 9 additions & 0 deletions docs/generated/packages/devkit/documents/nx_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ It only uses language primitives and immutable objects
- [NxAffectedConfig](../../devkit/documents/nx_devkit#nxaffectedconfig)
- [NxJsonConfiguration](../../devkit/documents/nx_devkit#nxjsonconfiguration)
- [NxPlugin](../../devkit/documents/nx_devkit#nxplugin)
- [PackageJson](../../devkit/documents/nx_devkit#packagejson)
- [ProjectConfiguration](../../devkit/documents/nx_devkit#projectconfiguration)
- [ProjectFileMap](../../devkit/documents/nx_devkit#projectfilemap)
- [ProjectGraph](../../devkit/documents/nx_devkit#projectgraph)
Expand Down Expand Up @@ -311,6 +312,14 @@ A plugin for Nx

---

### PackageJson

• **PackageJson**: `Object`

Package Json configuration

---

### ProjectConfiguration

• **ProjectConfiguration**: `Object`
Expand Down
13 changes: 9 additions & 4 deletions packages/eslint-plugin/src/rules/dependency-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { satisfies } from 'semver';
import { getHelperDependenciesFromProjectGraph } from '@nx/js';
import {
getAllDependencies,
getPackageJson,
removePackageJsonFromFileMap,
} from '../utils/package-json-utils';
import { JSONLiteral } from 'jsonc-eslint-parser/lib/parser/ast';
Expand Down Expand Up @@ -126,13 +127,17 @@ export default createESLintRule<Options, MessageIds>({
projectGraph
);

const rootPackageJson = getPackageJson(join(workspaceRoot, 'package.json'));

// find all dependencies for the project
const npmDeps = findProjectsNpmDependencies(
sourceProject,
projectGraph,
buildTarget,
rootPackageJson,
{
helperDependencies: helperDependencies.map((dep) => dep.target),
isProduction: true,
},
removePackageJsonFromFileMap(projectFileMap)
);
Expand All @@ -148,12 +153,12 @@ export default createESLintRule<Options, MessageIds>({
'package.json'
);

globalThis.projPackageJsonDeps ??= getAllDependencies(projPackageJsonPath);
globalThis.projPackageJsonDeps ??= getAllDependencies(
getPackageJson(projPackageJsonPath)
);
const projPackageJsonDeps: Record<string, string> =
globalThis.projPackageJsonDeps;
const rootPackageJsonDeps = getAllDependencies(
join(workspaceRoot, 'package.json')
);
const rootPackageJsonDeps = getAllDependencies(rootPackageJson);

function validateMissingDependencies(node: AST.JSONProperty) {
if (!checkMissingDependencies) {
Expand Down
23 changes: 14 additions & 9 deletions packages/eslint-plugin/src/utils/package-json-utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { ProjectFileMap, readJsonFile } from '@nx/devkit';
import { PackageJson, ProjectFileMap, readJsonFile } from '@nx/devkit';
import { existsSync } from 'fs';

export function getAllDependencies(path: string): Record<string, string> {
export function getAllDependencies(
packageJson: PackageJson
): Record<string, string> {
return {
...packageJson.dependencies,
...packageJson.devDependencies,
...packageJson.peerDependencies,
};
}

export function getPackageJson(path: string): PackageJson {
if (existsSync(path)) {
const packageJson = readJsonFile(path);
return {
...packageJson.dependencies,
...packageJson.devDependencies,
...packageJson.peerDependencies,
};
return readJsonFile(path);
}
return {};
return {} as PackageJson;
}

export function removePackageJsonFromFileMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ describe('updatePackageJson', () => {
const fileMap = {
'@org/lib1': [
{
file: 'test.ts',
file: 'libs/lib1/src/test.ts',
hash: '',
deps: ['npm:external1', 'npm:external2'],
},
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/devkit-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ export { readJsonFile, writeJsonFile } from './utils/fileutils';
*/
export { stripIndents } from './utils/strip-indents';

/**
* @category Utils
*/
export type { PackageJson } from './utils/package-json';
meeroslav marked this conversation as resolved.
Show resolved Hide resolved

/**
* @category Utils
*/
Expand Down
26 changes: 21 additions & 5 deletions packages/nx/src/plugins/js/package-json/create-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@ export function createPackageJson(
const projectNode = graph.nodes[projectName];
const isLibrary = projectNode.type === 'lib';

const rootPackageJson = readJsonFile(
`${options.root || workspaceRoot}/package.json`
);

const npmDeps = findProjectsNpmDependencies(
projectNode,
graph,
options.target,
{ helperDependencies: options.helperDependencies },
rootPackageJson,
{
helperDependencies: options.helperDependencies,
isProduction: options.isProduction,
},
fileMap
);

Expand Down Expand Up @@ -92,9 +100,6 @@ export function createPackageJson(
);
};

const rootPackageJson = readJsonFile(
`${options.root || workspaceRoot}/package.json`
);
Object.entries(npmDeps.dependencies).forEach(([packageName, version]) => {
if (
rootPackageJson.devDependencies?.[packageName] &&
Expand Down Expand Up @@ -179,9 +184,11 @@ export function findProjectsNpmDependencies(
projectNode: ProjectGraphProjectNode,
graph: ProjectGraph,
target: string,
rootPackageJson: PackageJson,
options: {
helperDependencies?: string[];
ignoredDependencies?: string[];
isProduction?: boolean;
},
fileMap?: ProjectFileMap
): NpmDeps {
Expand All @@ -208,13 +215,22 @@ export function findProjectsNpmDependencies(
recursivelyCollectPeerDependencies(dep, graph, npmDeps, seen);
});

// if it's production, we want to ignore all found devDependencies
const ignoredDependencies =
options.isProduction && rootPackageJson.devDependencies
? [
...(options.ignoredDependencies || []),
...Object.keys(rootPackageJson.devDependencies),
]
: options.ignoredDependencies || [];

findAllNpmDeps(
fileMap,
projectNode,
graph,
npmDeps,
seen,
options.ignoredDependencies || [],
ignoredDependencies,
dependencyInputs,
selfInputs
);
Expand Down
3 changes: 3 additions & 0 deletions packages/nx/src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export interface NxMigrationsConfiguration {

type PackageOverride = { [key: string]: string | PackageOverride };

/**
* Package Json configuration
*/
export interface PackageJson {
// Generic Package.Json Configuration
name: string;
Expand Down