feat: add --distDir arg to specify built plugin directory#2558
feat: add --distDir arg to specify built plugin directory#2558
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a --distDir option to @grafana/react-detect so the tool can locate built artifacts (sourcemaps, plugin.json) from a configurable output directory instead of assuming <pluginRoot>/dist.
Changes:
- Update
findSourceMapFilesandgetPluginJsonto operate on an explicitdistDirrather than appending/dist. - Thread
distDirthrough the detect command and results generation. - Add focused unit tests for the updated
file-scannerandgetPluginJsonbehavior.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/react-detect/src/utils/plugin.ts | Switch getPluginJson to accept distDir directly and add a cache reset helper. |
| packages/react-detect/src/utils/plugin.test.ts | New tests verifying getPluginJson reads from distDir without appending /dist. |
| packages/react-detect/src/results.ts | Change results generation to read plugin.json via distDir. |
| packages/react-detect/src/results.test.ts | Update tests to pass distDir into generateAnalysisResults. |
| packages/react-detect/src/file-scanner.ts | Update sourcemap scanning to use distDir as the glob cwd directly. |
| packages/react-detect/src/file-scanner.test.ts | New tests for findSourceMapFiles(distDir) behavior. |
| packages/react-detect/src/commands/detect19.ts | Add --distDir handling and pass it into scanning/results. |
| packages/react-detect/src/bin/run.ts | Register distDir as a parsed string CLI arg. |
Comments suppressed due to low confidence (1)
packages/react-detect/src/commands/detect19.ts:29
- When
--distDiris provided without--pluginRoot,pluginRootfalls back toprocess.cwd(), but dependency loading still runs againstpluginRoot(cwd). This makes the new “distDir-only” use case unreliable: dependency analysis may fail or silently be skipped (JSON mode suppresses the warning). Consider derivingpluginRootfromdistDirwhen--pluginRootis not set (e.g., walk up from distDir to find package.json/lockfile), or explicitly require--pluginRootfor dependency analysis and reflect that in behavior/docs.
let depContext: DependencyContext | null = null;
if (!skipDependencies) {
depContext = new DependencyContext();
try {
await depContext.loadDependencies(pluginRoot);
| export function getPluginJson(distDir: string) { | ||
| if (cachedPluginJson) { | ||
| return cachedPluginJson; | ||
| } | ||
|
|
There was a problem hiding this comment.
getPluginJson caches the parsed plugin.json globally without considering the distDir argument. If the function is called more than once in a single process with different distDir values (e.g., programmatic use, or multiple analyses), it can return the wrong plugin metadata. Consider keying the cache by distDir (e.g., Map<distDir, PluginJson>) or removing the cache (and dropping the need for resetPluginJsonCache).
There was a problem hiding this comment.
Fixed. Removed the cache entirely — is only called once per CLI run so the cache provided no benefit, and removing it eliminates the stale-data hazard with different values. is also gone.
| export function generateAnalysisResults( | ||
| matches: AnalyzedMatch[], | ||
| pluginRoot: string, | ||
| distDir: string, | ||
| depContext: DependencyContext | null, | ||
| options: AnalysisOptions = { skipBuildTooling: false, skipDependencies: false } |
There was a problem hiding this comment.
generateAnalysisResults now takes only distDir, but other parts of result generation still rely on process.cwd() (e.g., build-tooling checks via hasExternalisedJsxRuntime() and reported file paths). This means running the CLI from outside the plugin root (a key --distDir use case) can yield incorrect build-tooling detection and misleading file locations. Consider threading a pluginRoot/baseDir through results generation (separate from distDir) and using that instead of process.cwd() for config/file path resolution.
There was a problem hiding this comment.
Fixed. generateAnalysisResults now accepts pluginRoot as a second argument (after distDir). It is threaded through to hasExternalisedJsxRuntime(pluginRoot), generateResult(m, pluginRoot), and buildDependencyIssues(..., pluginRoot) — replacing all process.cwd() calls in the results path.
| // Reset module cache between tests since getPluginJson caches internally | ||
| beforeEach(async () => { | ||
| const { resetPluginJsonCache } = await import('./plugin.js'); | ||
| resetPluginJsonCache(); | ||
| }); |
There was a problem hiding this comment.
The comment says “Reset module cache between tests”, but the code is actually resetting getPluginJson’s internal cached value via resetPluginJsonCache() (the ESM module cache isn’t being cleared). Updating the comment to reflect what’s happening will avoid confusion for future test maintenance.
There was a problem hiding this comment.
Fixed. Updated to: // Reset getPluginJson's internal cached plugin.json value... actually, since we removed the cache entirely, the beforeEach is gone too. Tests now use a static import directly.
…h results generation
Summary
--distDirCLI argument to control wherereact-detectlooks for built artifacts (source maps,plugin.json)/distappend fromfindSourceMapFilesandgetPluginJson— they now use the provided path directly--distDirdefaults to<pluginRoot>/dist, so existing usage is unchangedTest Plan
npm run test -w @grafana/react-detect -- --runpasses (96 tests across 7 files)npm run typecheck -w @grafana/react-detectpassesnpx @grafana/react-detect --pluginRoot /path/to/pluginstill works (defaultdist/behaviour)npx @grafana/react-detect --distDir /path/to/plugin/custom-outputworks without--pluginRoot📦 Published PR as canary version:
Canary Versions✨ Test out this PR locally via:
npm install @grafana/react-detect@0.6.3-canary.2558.23893821583.0 # or yarn add @grafana/react-detect@0.6.3-canary.2558.23893821583.0