Skip to content

Conversation

@vrubezhny
Copy link
Contributor

Issue: #5086

@vrubezhny vrubezhny marked this pull request as draft June 17, 2025 03:33
@vrubezhny vrubezhny force-pushed the fix-transform-esm-dependencies branch 3 times, most recently from 050a49b to f6c796d Compare June 17, 2025 12:58
@vrubezhny vrubezhny force-pushed the fix-transform-esm-dependencies branch 25 times, most recently from 22ef19b to cc247df Compare July 2, 2025 15:08
Copy link
Contributor

@datho7561 datho7561 left a 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.

@vrubezhny vrubezhny force-pushed the fix-transform-esm-dependencies branch from 9c3a67e to 1e522ad Compare July 7, 2025 15:00
@vrubezhny
Copy link
Contributor Author

I can't get the dev mode to work properly; it complains about a missing problem matcher.

Any log message/error message screenshot?

@datho7561
Copy link
Contributor

screenshot of vscode with a popup that says the watch task doesn't have a problemMatcher

@vrubezhny vrubezhny force-pushed the fix-transform-esm-dependencies branch 2 times, most recently from 9ff28d7 to 9e5c527 Compare July 8, 2025 00:37
@vrubezhny
Copy link
Contributor Author

I can't get the dev mode to work properly; it complains about a missing problem matcher.

Fixed the watch command/task. Now it works for me without this "extra" dialog.

@vrubezhny vrubezhny requested a review from datho7561 July 8, 2025 00:56
tsconfig.json Outdated
"incremental": true,
"allowSyntheticDefaultImports": true,
"isolatedModules": true,
"types": [ "mocha", "node" ],
Copy link
Contributor

@datho7561 datho7561 Jul 8, 2025

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

Copy link
Contributor Author

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
}

Copy link
Contributor

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';

Copy link
Contributor

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
   },

Copy link
Contributor Author

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

Copy link
Contributor Author

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:

image

Copy link
Contributor Author

@vrubezhny vrubezhny Jul 9, 2025

Choose a reason for hiding this comment

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

Copy link
Contributor

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;
}

Copy link
Contributor Author

@vrubezhny vrubezhny Jul 9, 2025

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

Copy link
Contributor Author

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

Copy link
Contributor

@datho7561 datho7561 left a 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.

@vrubezhny vrubezhny force-pushed the fix-transform-esm-dependencies branch 8 times, most recently from 90636c3 to 58115cb Compare July 9, 2025 20:40
@vrubezhny vrubezhny requested a review from datho7561 July 10, 2025 00:09
@datho7561
Copy link
Contributor

datho7561 commented Jul 10, 2025

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",
Copy link
Contributor

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?

Copy link
Contributor Author

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

@vrubezhny vrubezhny force-pushed the fix-transform-esm-dependencies branch 2 times, most recently from b071dc5 to dbe8e80 Compare July 10, 2025 21:26
@vrubezhny vrubezhny force-pushed the fix-transform-esm-dependencies branch from dbe8e80 to 05285f8 Compare July 11, 2025 00:49
@vrubezhny vrubezhny requested a review from datho7561 July 11, 2025 01:51
Copy link
Contributor

@datho7561 datho7561 left a 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!

@vrubezhny vrubezhny merged commit 6b4090e into redhat-developer:main Jul 11, 2025
10 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants