Skip to content

Commit c4023b8

Browse files
committed
src/node/plugin.ts: Warn on duplicate plugin and only load first
1 parent d000853 commit c4023b8

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/node/plugin.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ import { version } from "./constants"
88
import * as util from "./util"
99
const fsp = fs.promises
1010

11-
// These fields are populated from the plugin's package.json.
1211
interface Plugin extends pluginapi.Plugin {
12+
/**
13+
* These fields are populated from the plugin's package.json.
14+
*/
1315
name: string
1416
version: string
1517
description: string
18+
19+
/**
20+
* path to the node module on the disk.
21+
*/
22+
modulePath: string
1623
}
1724

1825
interface Application extends pluginapi.Application {
@@ -47,7 +54,6 @@ export class PluginAPI {
4754
for (const p of this.plugins) {
4855
const pluginApps = await p.applications()
4956

50-
// TODO prevent duplicates
5157
// Add plugin key to each app.
5258
apps.push(
5359
...pluginApps.map((app) => {
@@ -112,8 +118,15 @@ export class PluginAPI {
112118
encoding: "utf8",
113119
})
114120
const packageJSON: PackageJSON = JSON.parse(str)
121+
for (const p of this.plugins) {
122+
if (p.name === packageJSON.name) {
123+
this.logger.warn(
124+
`ignoring duplicate plugin ${q(p.name)} at ${q(dir)}, using previously loaded ${q(p.modulePath)}`,
125+
)
126+
return
127+
}
128+
}
115129
const p = this._loadPlugin(dir, packageJSON)
116-
// TODO prevent duplicates
117130
this.plugins.push(p)
118131
} catch (err) {
119132
if (err.code !== "ENOENT") {
@@ -145,6 +158,7 @@ export class PluginAPI {
145158
name: packageJSON.name,
146159
version: packageJSON.version,
147160
description: packageJSON.description,
161+
modulePath: dir,
148162
...require(dir),
149163
} as Plugin
150164

test/plugin.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe("plugin", () => {
2525
name: "test-plugin",
2626
version: "1.0.0",
2727
description: "Fake plugin for testing code-server's plugin API",
28+
modulePath: path.join(__dirname, "test-plugin"),
2829
},
2930
},
3031
],

0 commit comments

Comments
 (0)