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: module resolution of linked plugins #352

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,13 @@ export class Config implements IConfig {
debug('runCommand %s %o', id, argv)
const c = cachedCommand || this.findCommand(id)
if (!c) {
await this.runHook('command_not_found', {id, argv})
const hookResult = await this.runHook('command_not_found', {id, argv})

if (hookResult.successes[0]) {
const cmdResult = hookResult.successes[0].result
return cmdResult as T
}

throw new CLIError(`command ${id} not found`)
}

Expand Down
2 changes: 1 addition & 1 deletion src/config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async function findRoot(name: string | undefined, root: string) {
if (name) {
let pkgPath
try {
pkgPath = resolvePackage(name, {paths: [__dirname, root]})
pkgPath = resolvePackage(name, {paths: [root]})
Copy link
Contributor

Choose a reason for hiding this comment

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

@mdonnalley why is __dirname being removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@peternhale two reasons: it's redundant and it was causing linked plugins to resolve to the version in node_modules - not your local version.

In the case of an unlinked plugin, we have
__dirname => /Users/mdonnalley/.nvm/versions/node/v17.1.0/lib/node_modules/@salesforce/cli/node_modules/@oclif/core/lib/config
root => /Users/mdonnalley/.nvm/versions/node/v17.1.0/lib/node_modules/@salesforce/cli

In this case, __dirname is redundant since it's already included by the root path

In the case of a linked plugin, we have

__dirname => /Users/mdonnalley/.nvm/versions/node/v17.1.0/lib/node_modules/@salesforce/cli/node_modules/@oclif/core/lib/config
root => /Users/mdonnalley/repos/oclif/plugin-version

In this case, __dirname was causing the resolution to return the node_modules version instead of the local version

In both cases, root is sufficient for resolving the plugin's location

} catch {}

return pkgPath ? findSourcesRoot(path.dirname(pkgPath)) : findRootLegacy(name, root)
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface Hooks {
};
'command_not_found': {
options: {id: string; argv?: string[]};
return: void;
return: unknown;
};
'plugins:preinstall': {
options: {
Expand Down