-
Notifications
You must be signed in to change notification settings - Fork 58
Enable using the ESM dependencies where possible #5145
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
Enable using the ESM dependencies where possible #5145
Conversation
050a49b to
f6c796d
Compare
22ef19b to
cc247df
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The built .vsix seems to work pretty well from my limited testing.
I can't get the dev mode to work properly; it complains about a missing problem matcher.
9c3a67e to
1e522ad
Compare
Any log message/error message screenshot? |
9ff28d7 to
9e5c527
Compare
Fixed the |
tsconfig.json
Outdated
| "incremental": true, | ||
| "allowSyntheticDefaultImports": true, | ||
| "isolatedModules": true, | ||
| "types": [ "mocha", "node" ], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to https://www.typescriptlang.org/tsconfig/#types, this means that only "@types/mocha" and "@types/node" will be used and all the other type definitions will be ignored. I think you should remove this line. It was causing errors for me when I opened the project in VS Code (most of the imported packages in kubeUtils.ts were marked as errors).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's problematic to remove this line as it causes the unit tests to fail at runtime because, when removed, no global types for Node.js nor globals from Mocha will be included, causing runtime error when running tests, like:
Error: Test failures: 1
at /home/user/projects/vscode-extension/test/unit/index.ts:84:33
[main 2025-07-08T15:38:18.031Z] [UtilityProcess id: 1, type: fileWatcher, pid: 660365]: crashed with code 15 and reason 'killed'
Exit code: 1
❌ Failed to run tests: TestRunFailedError: Test run failed with code 1
at ChildProcess.onProcessClosed (/home/user/projects/vscode-extension/node_modules/@vscode/test-electron/out/runTest.js:110:24)
at ChildProcess.emit (node:events:518:28)
at ChildProcess._handle.onexit (node:internal/child_process:293:12) {
code: 1,
signal: undefined
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that type error happens because this condition in ./test/unit/k8s/console.test.ts is wrong. I think you try to read the 'namespace' property of ctx if ctx is undefined:
project = (!ctx || !ctx.namespace) ? ctx.namespace : 'default';I think the condition should be this:
project = (Boolean(ctx) && Boolean(ctx.namespace)) ? ctx.namespace : 'default';There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try this patch:
diff --git a/src/k8s/console.ts b/src/k8s/console.ts
index 395a35b9..2c47964b 100644
--- a/src/k8s/console.ts
+++ b/src/k8s/console.ts
@@ -26,6 +26,9 @@ export class Console implements Disposable {
static getCurrentProject(): string {
const k8sConfigInfo = new KubeConfigInfo();
const k8sConfig = k8sConfigInfo.getEffectiveKubeConfig();
+ if (!k8sConfig.currentContext) {
+ return 'default';
+ }
const project = k8sConfig.contexts?.find((ctx) => ctx.name === k8sConfig.currentContext).namespace;
return project;
}
diff --git a/src/util/kubeUtils.ts b/src/util/kubeUtils.ts
index 979bd01e..f2b3885d 100644
--- a/src/util/kubeUtils.ts
+++ b/src/util/kubeUtils.ts
@@ -3,7 +3,7 @@
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
-import { KubeConfig, loadYaml } from '@kubernetes/client-node';
+import { KubeConfig, loadYaml } from '@kubernetes/client-node/dist';
import { ActionOnInvalid, Cluster, Context, User } from '@kubernetes/client-node/dist/config_types';
import * as fs from 'fs';
import * as path from 'path';
diff --git a/test/unit/k8s/console.test.ts b/test/unit/k8s/console.test.ts
index 371c33ab..6c8b75d9 100644
--- a/test/unit/k8s/console.test.ts
+++ b/test/unit/k8s/console.test.ts
@@ -38,7 +38,7 @@ suite('K8s/console', () => {
const k8sConfigInfo = new KubeConfigInfo();
const k8sConfig = k8sConfigInfo.getEffectiveKubeConfig();
const ctx = k8sConfig.contexts.find((ctx) => ctx.name === k8sConfig.currentContext);
- project = (!ctx || !ctx.namespace) ? ctx.namespace : 'default';
+ project = (ctx && ctx.namespace) ? ctx.namespace : 'default';
});
teardown(() => {
diff --git a/tsconfig.json b/tsconfig.json
index 8246e3c2..70f3cccb 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -28,7 +28,6 @@
"incremental": true,
"allowSyntheticDefaultImports": true,
"isolatedModules": true,
- "types": [ "mocha", "node" ],
"typeRoots": [ "./src/@types", "./node_modules/@types", "./out/extension" ],
"sourceMap": true
},There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How this is going to fix
diff --git a/src/k8s/console.ts b/src/k8s/console.ts index 395a35b9..2c47964b 100644 --- a/src/k8s/console.ts +++ b/src/k8s/console.ts @@ -26,6 +26,9 @@ export class Console implements Disposable { static getCurrentProject(): string { const k8sConfigInfo = new KubeConfigInfo(); const k8sConfig = k8sConfigInfo.getEffectiveKubeConfig(); + if (!k8sConfig.currentContext) { + return 'default'; + } const project = k8sConfig.contexts?.find((ctx) => ctx.name === k8sConfig.currentContext).namespace; return project; }
I don't think it's a good idea to always return 'default' context if it's not actually set. Most likely this context will never exist...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your patch works for me (the only thing I haven't changed is
-import { KubeConfig, loadYaml } from '@kubernetes/client-node';
+import { KubeConfig, loadYaml } from '@kubernetes/client-node/dist';
in kubeUtils.ts. I don't think it's needed as these types provided by the ESM-to-CJS-converted module, while, if you import anything from @kubernetes/client-node/dist, you peek it from native ESM installed in node_modules which is not good and might bring fail to load ESM from CJS-like problem.
An I still see some import not found errors in the editor:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope. Your full patch doesn't work on Github CI... f.i: https://github.com/redhat-developer/vscode-openshift-tools/actions/runs/16157145086/job/45601841296?pr=5145
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How this is going to fix
You're right, it doesn't fix it properly.
Based on the test results, I think that they are expecting 'default' in the case that there's no current context, or the current context is pointing to a value that doesn't exist. This code for getCurrentProject in console.ts should handle that properly:
static getCurrentProject(): string {
const k8sConfigInfo = new KubeConfigInfo();
const k8sConfig = k8sConfigInfo.getEffectiveKubeConfig();
if (k8sConfig.currentContext === undefined) {
return 'default';
}
const project = k8sConfig.contexts?.find((ctx) => ctx.name === k8sConfig.currentContext)?.namespace;
if (project === undefined) {
return 'default';
}
return project;
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're searching for a project, which is to be default in case it's not set in the current context... But the currentContext should not be replaced with any value (including 'default') in case it's not set - it should stay 'undefined' or 'null'.
I mean this should drop down to:
if (project === undefined) {
return 'default';
}
and finally return 'default'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following changes:
// src/k8s/console.ts
static getCurrentProject(): string {
const k8sConfigInfo = new KubeConfigInfo();
const k8sConfig = k8sConfigInfo.getEffectiveKubeConfig();
if (k8sConfig.currentContext === undefined) {
return undefined;
}
return k8sConfig.contexts?.find((ctx) => ctx.name === k8sConfig.currentContext).namespace;
}
// test/unit/k8s/comsole.test.ts
setup(() => {
sandbox = sinon.createSandbox();
cliExecStub = sandbox.stub(CliChannel.prototype, 'executeTool').resolves({ stdout: '', stderr: undefined, error: undefined});
commandStub = sandbox.stub(vscode.commands, 'executeCommand');
const k8sConfigInfo = new KubeConfigInfo();
const k8sConfig = k8sConfigInfo.getEffectiveKubeConfig();
const ctx = k8sConfig.contexts.find((ctx) => ctx.name === k8sConfig.currentContext);
project = (ctx && ctx.namespace) ? ctx.namespace : undefined;
});
... made the test pass OK.
We don't need to return any "replacement" value for project in case it cannot be found - we should return 'undefined'... It's the due of application explorer to make some assumptions and try finding the current project name (based on the cluster API, etc.).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, it seems to be mostly working. I've added one comment about the tsconfig.json that needs to be addressed, and it needs to be rebased, but after that I think it's good to merge.
90636c3 to
58115cb
Compare
|
When running in the "Dev" mode where I launch by pressing "F5", I tried creating a component then doing "Start Dev". The terminal that shows the output of "Start Dev" doesn't appear to work. When I use the .vsix it works properly. |
| "eslint-plugin-json": "^4.0.1", | ||
| "eslint-plugin-prettier": "^5.5.1", | ||
| "express": "^5.1.0", | ||
| "fast-glob": "^3.3.3", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for using fast-glob? glob is already required by other dependencies, so you could use it, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are glob version conflicts between the glob version we use, the version brought by some transitive deps and the one used by 'shelljs' (which I can only make 'external' in esbuild config:
external: [ 'vscode', 'shelljs', 'jsonc-parser' ],
Also, if I recall it right, I had some problems while running some build/*.cjs scripts that depend/use 'glob', so I decided to get rid of glob and shelljs (we need the only which function from it) and start using fast-glob instead...
See: #5195
b071dc5 to
dbe8e80
Compare
Issue: redhat-developer#5086 Signed-off-by: Victor Rubezhny <[email protected]>
dbe8e80 to
05285f8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, works well. Thanks, Victor!


Issue: #5086