Repository navigation
fix(eslint-plugin): validate workspace dependencies without builds #37105
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
Open
Paul Mardling (PaulGMardling)
wants to merge
12
commits into
microsoft:master
Choose a base branch
from
PaulGMardling:fix/27727-lint-dependency-resolution
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b05e3ad
chore(react-text): pilot build-independent dependency linting
PaulGMardling 9106a15
Merge master and preserve dependency lint pilot documentation
PaulGMardling 697e963
fix(react-text): clarify dependency lint prerequisites and recovery
PaulGMardling dffc2f4
Merge branch 'master' into fix/27727-lint-dependency-resolution
PaulGMardling 787e253
Merge branch 'master' into fix/27727-lint-dependency-resolution
PaulGMardling 749d060
fix(eslint-plugin): validate workspace dependencies without builds
PaulGMardling 514d093
fix(lint): preserve shared plugin composition and cache freshness
PaulGMardling 4c11aa9
Merge branch 'master' into fix/27727-lint-dependency-resolution
PaulGMardling 0f02b0c
fix(lint): refresh workspace snapshots without repeated discovery
PaulGMardling 6ea1f7f
refactor(eslint-plugin): simplify workspace lint implementation and t…
PaulGMardling 2f85006
Merge branch 'master' into fix/27727-lint-dependency-resolution
PaulGMardling 06e7b4c
Merge branch 'master' into fix/27727-lint-dependency-resolution
PaulGMardling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
change/@fluentui-react-text-932fc45f-fc19-4898-ad7d-4debde6037e0.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "none", | ||
| "comment": "chore(react-text): add an opt-in build-independent dependency lint pilot", | ||
| "packageName": "@fluentui/react-text", | ||
| "email": "paulmardling@microsoft.com" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
packages/react-components/react-text/library/config/dependency-lint.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| /** @jest-environment node */ | ||
|
|
||
| const { Linter } = require('eslint'); | ||
| const importPlugin = require('eslint-plugin-import'); | ||
| const { readCachedProjectGraph, workspaceRoot } = require('@nx/devkit'); | ||
| const { readFileMapCache } = require('nx/src/project-graph/nx-deps-cache'); | ||
| const { createProjectRootMappings } = require('nx/src/project-graph/utils/find-project-for-path'); | ||
| const { join, relative } = require('node:path'); | ||
| const { mkdtempSync, writeFileSync, mkdirSync, rmSync } = require('node:fs'); | ||
| const { tmpdir } = require('node:os'); | ||
| const { spawnSync } = require('node:child_process'); | ||
|
|
||
| const config = require('./eslint.config.cjs'); | ||
| const manifest = require('../package.json'); | ||
| const projectRoot = 'packages/react-components/react-text/library'; | ||
|
|
||
| describe('dependency lint pilot', () => { | ||
| const graphGlobals = ['projectGraph', 'projectFileMap', 'projectRootMappings', 'targetProjectLocator']; | ||
| let originalGlobals; | ||
| let graph; | ||
| let fileMap; | ||
|
|
||
| beforeEach(() => { | ||
| originalGlobals = Object.fromEntries(graphGlobals.map(key => [key, globalThis[key]])); | ||
| graph = JSON.parse(JSON.stringify(readCachedProjectGraph())); | ||
| fileMap = JSON.parse(JSON.stringify(readFileMapCache().fileMap.projectFileMap)); | ||
| globalThis.projectGraph = graph; | ||
| globalThis.projectFileMap = fileMap; | ||
| globalThis.projectRootMappings = createProjectRootMappings(graph.nodes); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| for (const key of graphGlobals) { | ||
| if (originalGlobals[key] === undefined) { | ||
| delete globalThis[key]; | ||
| } else { | ||
| globalThis[key] = originalGlobals[key]; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| function lint(packageJson) { | ||
| return new Linter().verify(JSON.stringify(packageJson), config, { | ||
| filename: join(workspaceRoot, projectRoot, 'package.json'), | ||
| }); | ||
| } | ||
|
|
||
| function addImport(file, dependency) { | ||
| fileMap['react-text'].push({ file: `${projectRoot}/${file}`, hash: 'fixture', deps: [dependency] }); | ||
| } | ||
|
|
||
| it('accepts the current manifest without building component packages', () => { | ||
| expect(lint(manifest)).toEqual([]); | ||
| }); | ||
|
|
||
| it.each(['@fluentui/react-theme', '@griffel/react', '@swc/helpers'])( | ||
| 'reports a missing production dependency: %s', | ||
| dependency => { | ||
| const packageJson = JSON.parse(JSON.stringify(manifest)); | ||
| delete packageJson.dependencies[dependency]; | ||
| const messages = lint(packageJson); | ||
| expect(messages.length).toBeGreaterThan(0); | ||
| expect(messages.every(message => message.ruleId === '@nx/dependency-checks')).toBe(true); | ||
| expect(messages.some(message => message.message.includes(dependency))).toBe(true); | ||
| }, | ||
| ); | ||
|
|
||
| describe.each(['@fluentui/react-theme', '@griffel/react'])('%s declarations', dependency => { | ||
| it('does not accept a runtime dependency declared only as a devDependency', () => { | ||
| const packageJson = JSON.parse(JSON.stringify(manifest)); | ||
| packageJson.devDependencies = { | ||
| ...packageJson.devDependencies, | ||
| [dependency]: packageJson.dependencies[dependency], | ||
| }; | ||
| delete packageJson.dependencies[dependency]; | ||
| expect(lint(packageJson).some(message => message.message.includes(dependency))).toBe(true); | ||
| }); | ||
|
|
||
| it.each(['peerDependencies', 'optionalDependencies'])('accepts a runtime declaration in %s', section => { | ||
| const packageJson = JSON.parse(JSON.stringify(manifest)); | ||
| packageJson[section] = { | ||
| ...packageJson[section], | ||
| [dependency]: packageJson.dependencies[dependency], | ||
| }; | ||
| delete packageJson.dependencies[dependency]; | ||
| expect(lint(packageJson)).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| it('does not exempt external runtime imports installed as root devDependencies', () => { | ||
| addImport('src/runtime.ts', 'npm:lodash'); | ||
| expect(lint(manifest).some(message => message.message.includes('lodash'))).toBe(true); | ||
| }); | ||
|
|
||
| it.each(['src/fixture.test.ts', 'src/testing/fixture.ts', 'config/fixture.cjs'])( | ||
| 'ignores development imports from %s', | ||
| file => { | ||
| addImport(file, 'npm:lodash'); | ||
| expect(lint(manifest)).toEqual([]); | ||
| }, | ||
| ); | ||
|
|
||
| it('does not introduce obsolete dependency or version-range enforcement', () => { | ||
| const packageJson = JSON.parse(JSON.stringify(manifest)); | ||
| packageJson.dependencies.lodash = '0.0.0'; | ||
| packageJson.dependencies['@griffel/react'] = '0.0.0'; | ||
| expect(lint(packageJson)).toEqual([]); | ||
| }); | ||
|
|
||
| it('records the unsupported generated workspace JSX runtime case', () => { | ||
| const packageJson = JSON.parse(JSON.stringify(manifest)); | ||
| delete packageJson.dependencies['@fluentui/react-jsx-runtime']; | ||
| expect(lint(packageJson)).toEqual([]); | ||
| }); | ||
|
|
||
| it('reports the same missing workspace dependency before and after its entry point exists', () => { | ||
| const directory = mkdtempSync(join(tmpdir(), 'react-text-dependency-lint-')); | ||
| try { | ||
| const dependencyRoot = join(directory, 'node_modules/@fixture/dependency'); | ||
| mkdirSync(dependencyRoot, { recursive: true }); | ||
| mkdirSync(join(directory, 'src')); | ||
| writeFileSync(join(directory, 'package.json'), JSON.stringify({ name: '@fixture/consumer', version: '1.0.0' })); | ||
| writeFileSync( | ||
| join(dependencyRoot, 'package.json'), | ||
| JSON.stringify({ name: '@fixture/dependency', version: '1.0.0', main: 'lib/index.js' }), | ||
| ); | ||
| graph.nodes['dependency-lint-fixture'] = { | ||
| name: 'dependency-lint-fixture', | ||
| type: 'lib', | ||
| data: { root: relative(workspaceRoot, dependencyRoot), targets: { build: { executor: 'nx:noop' } } }, | ||
| }; | ||
| addImport('src/runtime.ts', 'dependency-lint-fixture'); | ||
| const lintImport = () => | ||
| new Linter({ cwd: directory }).verify( | ||
| 'import { value } from "@fixture/dependency";', | ||
| [ | ||
| { | ||
| plugins: { import: importPlugin }, | ||
| rules: { 'import/no-extraneous-dependencies': ['error', { packageDir: directory }] }, | ||
| }, | ||
| ], | ||
| { filename: join(directory, 'src/index.js') }, | ||
| ); | ||
| expect(lintImport()).toEqual([]); | ||
| const before = lint(manifest); | ||
| expect(before.some(message => message.message.includes('@fixture/dependency'))).toBe(true); | ||
|
|
||
| mkdirSync(join(dependencyRoot, 'lib')); | ||
| writeFileSync(join(dependencyRoot, 'lib/index.js'), 'exports.value = 1;'); | ||
| expect(lintImport().map(message => message.ruleId)).toEqual(['import/no-extraneous-dependencies']); | ||
| expect(lint(manifest)).toEqual(before); | ||
| expect(lint({ ...manifest, dependencies: { ...manifest.dependencies, '@fixture/dependency': '1.0.0' } })).toEqual( | ||
| [], | ||
| ); | ||
| } finally { | ||
| rmSync(directory, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it('fails instead of skipping the check when the Nx graph is missing', () => { | ||
| const directory = mkdtempSync(join(tmpdir(), 'react-text-dependency-graph-')); | ||
| try { | ||
| const result = spawnSync(process.execPath, ['-e', 'require("./config/eslint.config.cjs")'], { | ||
| cwd: join(workspaceRoot, projectRoot), | ||
| env: { ...process.env, NX_WORKSPACE_DATA_DIRECTORY: directory }, | ||
| encoding: 'utf8', | ||
| }); | ||
| expect(result.error).toBeUndefined(); | ||
| expect(result.status).not.toBe(0); | ||
| expect(result.stderr).toContain('No cached ProjectGraph is available'); | ||
| } finally { | ||
| rmSync(directory, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it.each(['project', 'build target', 'file map'])('fails when the cached %s is missing', missing => { | ||
| const directory = mkdtempSync(join(tmpdir(), 'react-text-dependency-graph-')); | ||
| try { | ||
| if (missing === 'project') { | ||
| delete graph.nodes['react-text']; | ||
| } else if (missing === 'build target') { | ||
| delete graph.nodes['react-text'].data.targets.build; | ||
| } | ||
| writeFileSync(join(directory, 'project-graph.json'), JSON.stringify(graph)); | ||
| if (missing !== 'file map') { | ||
| writeFileSync(join(directory, 'file-map.json'), JSON.stringify({ fileMap: { projectFileMap: fileMap } })); | ||
| } | ||
| const result = spawnSync(process.execPath, ['-e', 'require("./config/eslint.config.cjs")'], { | ||
| cwd: join(workspaceRoot, projectRoot), | ||
| env: { ...process.env, NX_WORKSPACE_DATA_DIRECTORY: directory }, | ||
| encoding: 'utf8', | ||
| }); | ||
| expect(result.error).toBeUndefined(); | ||
| expect(result.status).not.toBe(0); | ||
| expect(result.stderr).toContain('Dependency lint requires the react-text project graph and file map'); | ||
| } finally { | ||
| rmSync(directory, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
| }); |
33 changes: 33 additions & 0 deletions
33
packages/react-components/react-text/library/config/eslint.config.cjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // @ts-check | ||
|
|
||
| const { readCachedProjectGraph } = require('@nx/devkit'); | ||
| const nxPlugin = require('@nx/eslint-plugin'); | ||
| const jsonParser = require('jsonc-eslint-parser'); | ||
| const { readFileMapCache } = require('nx/src/project-graph/nx-deps-cache'); | ||
|
|
||
| // Nx otherwise warns and skips dependency checking when its graph cache is missing. | ||
| const graph = readCachedProjectGraph(); | ||
| if (!graph.nodes['react-text']?.data.targets?.build || !readFileMapCache()?.fileMap.projectFileMap['react-text']) { | ||
| throw new Error( | ||
| 'Dependency lint requires the react-text project graph and file map. Run yarn nx run react-text:lint-dependencies.', | ||
|
PaulGMardling marked this conversation as resolved.
Resolved
|
||
| ); | ||
| } | ||
|
PaulGMardling marked this conversation as resolved.
Resolved
|
||
|
|
||
| module.exports = [ | ||
| { | ||
| files: ['**/package.json'], | ||
| languageOptions: { parser: jsonParser }, | ||
| plugins: { '@nx': nxPlugin }, | ||
| rules: { | ||
| '@nx/dependency-checks': [ | ||
| 'error', | ||
| { | ||
| checkObsoleteDependencies: false, | ||
| checkVersionMismatches: false, | ||
| ignoredFiles: ['{projectRoot}/config/**', '{projectRoot}/src/testing/**'], | ||
| runtimeHelpers: ['@swc/helpers'], | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.