-
Notifications
You must be signed in to change notification settings - Fork 653
[rush-lib] Add PathProjectSelectorParser with path: scheme and auto-routing #5451
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
Merged
+518
−41
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6089245
Initial plan
Copilot 52cb5fa
Add PathProjectSelectorParser implementation with path: scheme
Copilot d54f4bb
Address code review feedback and add comprehensive unit tests for sel…
Copilot 2b2fcf9
Add tests for VersionPolicyProjectSelectorParser and SubspaceSelector…
Copilot 24cdd9c
Refactor tests into separate files and improve PathProjectSelectorPar…
Copilot b4f2e53
Add change file for PathProjectSelectorParser feature
Copilot b597b97
Update common/changes/@microsoft/rush/copilot-add-path-project-select…
dmichon-msft 395cc75
Address code review feedback: make workingDirectory required and add …
Copilot 4967774
Fix Windows path separator issue by normalizing to forward slashes fo…
Copilot 77daa17
Use consistent cwd from RushCommandLineParser for path resolution
Copilot b70d1a7
Auto-route relative and absolute POSIX paths to path: selector
Copilot 52c2340
Use Path.convertToSlashes helper instead of manual path separator con…
Copilot f602e38
Rush change.
iclanton 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
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
65 changes: 65 additions & 0 deletions
65
libraries/rush-lib/src/logic/selectors/PathProjectSelectorParser.ts
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,65 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| import * as nodePath from 'node:path'; | ||
|
|
||
| import { AlreadyReportedError } from '@rushstack/node-core-library'; | ||
| import type { LookupByPath } from '@rushstack/lookup-by-path'; | ||
|
|
||
| import type { RushConfiguration } from '../../api/RushConfiguration'; | ||
| import type { RushConfigurationProject } from '../../api/RushConfigurationProject'; | ||
| import type { IEvaluateSelectorOptions, ISelectorParser } from './ISelectorParser'; | ||
| import { RushConstants } from '../RushConstants'; | ||
|
|
||
| export class PathProjectSelectorParser implements ISelectorParser<RushConfigurationProject> { | ||
| private readonly _rushConfiguration: RushConfiguration; | ||
|
|
||
| public constructor(rushConfiguration: RushConfiguration) { | ||
| this._rushConfiguration = rushConfiguration; | ||
| } | ||
|
|
||
| public async evaluateSelectorAsync({ | ||
| unscopedSelector, | ||
| terminal, | ||
| parameterName | ||
| }: IEvaluateSelectorOptions): Promise<Iterable<RushConfigurationProject>> { | ||
| // Resolve the input path against the working directory | ||
| const absolutePath: string = nodePath.resolve(process.cwd(), unscopedSelector); | ||
|
|
||
| // Relativize it to the rushJsonFolder | ||
| const relativePath: string = nodePath.relative(this._rushConfiguration.rushJsonFolder, absolutePath); | ||
|
|
||
| // Get the LookupByPath instance for the Rush root | ||
| const lookupByPath: LookupByPath<RushConfigurationProject> = | ||
| this._rushConfiguration.getProjectLookupForRoot(this._rushConfiguration.rushJsonFolder); | ||
|
|
||
| // Check if this path is within a project or matches a project exactly | ||
| const containingProject: RushConfigurationProject | undefined = lookupByPath.findChildPath(relativePath); | ||
|
|
||
| if (containingProject) { | ||
| return [containingProject]; | ||
| } | ||
|
|
||
| // Check if there are any projects under this path (i.e., it's a directory containing projects) | ||
| const projectsUnderPath: Set<RushConfigurationProject> = new Set(); | ||
| for (const [, project] of lookupByPath.entries(relativePath)) { | ||
| projectsUnderPath.add(project); | ||
| } | ||
|
|
||
| if (projectsUnderPath.size > 0) { | ||
| return projectsUnderPath; | ||
| } | ||
|
|
||
| // No projects found | ||
| terminal.writeErrorLine( | ||
| `The path "${unscopedSelector}" passed to "${parameterName}" does not match any project in ` + | ||
| `${RushConstants.rushJsonFilename}. The resolved path relative to the Rush root is "${relativePath}".` | ||
| ); | ||
| throw new AlreadyReportedError(); | ||
| } | ||
|
|
||
| public getCompletions(): Iterable<string> { | ||
| // Return empty completions as path completions are typically handled by the shell | ||
| return []; | ||
| } | ||
| } |
195 changes: 195 additions & 0 deletions
195
libraries/rush-lib/src/logic/selectors/test/SelectorParsers.test.ts
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,195 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| import * as path from 'node:path'; | ||
|
|
||
| import { StringBufferTerminalProvider, Terminal } from '@rushstack/terminal'; | ||
|
|
||
| import { RushConfiguration } from '../../../api/RushConfiguration'; | ||
| import { NamedProjectSelectorParser } from '../NamedProjectSelectorParser'; | ||
| import { TagProjectSelectorParser } from '../TagProjectSelectorParser'; | ||
| import { PathProjectSelectorParser } from '../PathProjectSelectorParser'; | ||
|
|
||
| describe('SelectorParsers', () => { | ||
| let rushConfiguration: RushConfiguration; | ||
| let terminal: Terminal; | ||
| let terminalProvider: StringBufferTerminalProvider; | ||
|
|
||
| beforeEach(() => { | ||
| const rushJsonFile: string = path.resolve(__dirname, '../../../api/test/repo/rush-npm.json'); | ||
| rushConfiguration = RushConfiguration.loadFromConfigurationFile(rushJsonFile); | ||
| terminalProvider = new StringBufferTerminalProvider(); | ||
| terminal = new Terminal(terminalProvider); | ||
| }); | ||
|
|
||
| describe(NamedProjectSelectorParser.name, () => { | ||
| let parser: NamedProjectSelectorParser; | ||
|
|
||
| beforeEach(() => { | ||
| parser = new NamedProjectSelectorParser(rushConfiguration); | ||
| }); | ||
|
|
||
| it('should select a project by exact package name', async () => { | ||
| const result = await parser.evaluateSelectorAsync({ | ||
| unscopedSelector: 'project1', | ||
| terminal, | ||
| parameterName: '--only' | ||
| }); | ||
|
|
||
| const projects = Array.from(result); | ||
| expect(projects).toHaveLength(1); | ||
| expect(projects[0].packageName).toBe('project1'); | ||
| }); | ||
|
|
||
| it('should throw error for non-existent project', async () => { | ||
| await expect( | ||
| parser.evaluateSelectorAsync({ | ||
| unscopedSelector: 'nonexistent', | ||
| terminal, | ||
| parameterName: '--only' | ||
| }) | ||
| ).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('should provide completions for all projects', () => { | ||
| const completions = Array.from(parser.getCompletions()); | ||
| expect(completions).toContain('project1'); | ||
| expect(completions).toContain('project2'); | ||
| expect(completions).toContain('project3'); | ||
| }); | ||
| }); | ||
|
|
||
| describe(TagProjectSelectorParser.name, () => { | ||
| let parser: TagProjectSelectorParser; | ||
|
|
||
| beforeEach(() => { | ||
| parser = new TagProjectSelectorParser(rushConfiguration); | ||
| }); | ||
|
|
||
| it('should return empty array for projects without tags', () => { | ||
| // The test fixture doesn't have tags configured, so this should return empty | ||
dmichon-msft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const completions = Array.from(parser.getCompletions()); | ||
| expect(completions).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should throw error for non-existent tag', async () => { | ||
| await expect( | ||
| parser.evaluateSelectorAsync({ | ||
| unscopedSelector: 'nonexistent-tag', | ||
| terminal, | ||
| parameterName: '--only' | ||
| }) | ||
| ).rejects.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe(PathProjectSelectorParser.name, () => { | ||
| let parser: PathProjectSelectorParser; | ||
| const originalCwd = process.cwd(); | ||
|
|
||
| beforeEach(() => { | ||
| parser = new PathProjectSelectorParser(rushConfiguration); | ||
dmichon-msft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Set cwd to the test repo root for consistent path resolution | ||
| process.chdir(rushConfiguration.rushJsonFolder); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.chdir(originalCwd); | ||
| }); | ||
|
|
||
| it('should select a project by exact path', async () => { | ||
| const result = await parser.evaluateSelectorAsync({ | ||
| unscopedSelector: 'project1', | ||
| terminal, | ||
| parameterName: '--only' | ||
| }); | ||
|
|
||
| const projects = Array.from(result); | ||
| expect(projects).toHaveLength(1); | ||
| expect(projects[0].packageName).toBe('project1'); | ||
| }); | ||
|
|
||
| it('should select a project by path within the project', async () => { | ||
| const result = await parser.evaluateSelectorAsync({ | ||
| unscopedSelector: 'project1/src/index.ts', | ||
| terminal, | ||
| parameterName: '--only' | ||
| }); | ||
|
|
||
| const projects = Array.from(result); | ||
| expect(projects).toHaveLength(1); | ||
| expect(projects[0].packageName).toBe('project1'); | ||
| }); | ||
|
|
||
| it('should select multiple projects from a parent directory', async () => { | ||
| const result = await parser.evaluateSelectorAsync({ | ||
| unscopedSelector: '.', | ||
| terminal, | ||
| parameterName: '--only' | ||
| }); | ||
|
|
||
| const projects = Array.from(result); | ||
| expect(projects.length).toBeGreaterThan(0); | ||
| // Should include all projects in the test repo | ||
| const packageNames = projects.map((p) => p.packageName).sort(); | ||
| expect(packageNames).toContain('project1'); | ||
| expect(packageNames).toContain('project2'); | ||
| expect(packageNames).toContain('project3'); | ||
| }); | ||
|
|
||
| it('should select project from current directory when using dot', async () => { | ||
| // Change to project1 directory | ||
| process.chdir(path.join(rushConfiguration.rushJsonFolder, 'project1')); | ||
|
|
||
| const result = await parser.evaluateSelectorAsync({ | ||
| unscopedSelector: '.', | ||
| terminal, | ||
| parameterName: '--only' | ||
| }); | ||
|
|
||
| const projects = Array.from(result); | ||
| expect(projects).toHaveLength(1); | ||
| expect(projects[0].packageName).toBe('project1'); | ||
| }); | ||
|
|
||
| it('should handle absolute paths', async () => { | ||
| const absolutePath = path.join(rushConfiguration.rushJsonFolder, 'project2'); | ||
|
|
||
| const result = await parser.evaluateSelectorAsync({ | ||
| unscopedSelector: absolutePath, | ||
| terminal, | ||
| parameterName: '--only' | ||
| }); | ||
|
|
||
| const projects = Array.from(result); | ||
| expect(projects).toHaveLength(1); | ||
| expect(projects[0].packageName).toBe('project2'); | ||
| }); | ||
|
|
||
| it('should throw error for paths that do not match any project', async () => { | ||
| await expect( | ||
| parser.evaluateSelectorAsync({ | ||
| unscopedSelector: 'nonexistent/path', | ||
| terminal, | ||
| parameterName: '--only' | ||
| }) | ||
| ).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('should handle paths outside workspace', async () => { | ||
| // Paths outside the workspace should not match any project and throw | ||
| await expect( | ||
| parser.evaluateSelectorAsync({ | ||
| unscopedSelector: '../outside', | ||
| terminal, | ||
| parameterName: '--only' | ||
| }) | ||
| ).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('should return empty completions', () => { | ||
| const completions = Array.from(parser.getCompletions()); | ||
| expect(completions).toHaveLength(0); | ||
| }); | ||
| }); | ||
| }); | ||
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.