Skip to content

feat: add --distDir arg to specify built plugin directory#2558

Open
jackw wants to merge 4 commits intomainfrom
jackw/react-detect-dist-dir
Open

feat: add --distDir arg to specify built plugin directory#2558
jackw wants to merge 4 commits intomainfrom
jackw/react-detect-dist-dir

Conversation

@jackw
Copy link
Copy Markdown
Collaborator

@jackw jackw commented Apr 2, 2026

Summary

  • Adds a --distDir CLI argument to control where react-detect looks for built artifacts (source maps, plugin.json)
  • Removes the hardcoded /dist append from findSourceMapFiles and getPluginJson — they now use the provided path directly
  • --distDir defaults to <pluginRoot>/dist, so existing usage is unchanged

Test Plan

  • npm run test -w @grafana/react-detect -- --run passes (96 tests across 7 files)
  • npm run typecheck -w @grafana/react-detect passes
  • npx @grafana/react-detect --pluginRoot /path/to/plugin still works (default dist/ behaviour)
  • npx @grafana/react-detect --distDir /path/to/plugin/custom-output works 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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

Hello! 👋 This repository uses Auto for releasing packages using PR labels.

✨ This PR can be merged and will trigger a new patch release.
NOTE: When merging a PR with the release label please avoid merging another PR. For further information see here.

@github-project-automation github-project-automation bot moved this from 📬 Triage to 🔬 In review in Grafana Catalog Team Apr 2, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 findSourceMapFiles and getPluginJson to operate on an explicit distDir rather than appending /dist.
  • Thread distDir through the detect command and results generation.
  • Add focused unit tests for the updated file-scanner and getPluginJson behavior.

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 --distDir is provided without --pluginRoot, pluginRoot falls back to process.cwd(), but dependency loading still runs against pluginRoot (cwd). This makes the new “distDir-only” use case unreliable: dependency analysis may fail or silently be skipped (JSON mode suppresses the warning). Consider deriving pluginRoot from distDir when --pluginRoot is not set (e.g., walk up from distDir to find package.json/lockfile), or explicitly require --pluginRoot for dependency analysis and reflect that in behavior/docs.
    let depContext: DependencyContext | null = null;
    if (!skipDependencies) {
      depContext = new DependencyContext();
      try {
        await depContext.loadDependencies(pluginRoot);

Comment on lines 21 to 25
export function getPluginJson(distDir: string) {
if (cachedPluginJson) {
return cachedPluginJson;
}

Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 13 to 17
export function generateAnalysisResults(
matches: AnalyzedMatch[],
pluginRoot: string,
distDir: string,
depContext: DependencyContext | null,
options: AnalysisOptions = { skipBuildTooling: false, skipDependencies: false }
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +6 to +10
// Reset module cache between tests since getPluginJson caches internally
beforeEach(async () => {
const { resetPluginJsonCache } = await import('./plugin.js');
resetPluginJsonCache();
});
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jackw jackw added patch Increment the patch version when merged release Create a release when this pr is merged labels Apr 2, 2026
@jackw jackw changed the title fix(react-detect): add --distDir arg to decouple built output path from pluginRoot feat: add --distDir arg to specify built plugin directory Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patch Increment the patch version when merged release Create a release when this pr is merged

Projects

Status: 🔬 In review

Development

Successfully merging this pull request may close these issues.

3 participants