From dba707a3915e6c94f72ed50cf906a48742ba301f Mon Sep 17 00:00:00 2001 From: Matt Seddon Date: Fri, 19 May 2023 13:48:20 +1000 Subject: [PATCH] Fix extension initialization on Windows (downgrade from esm only packages) --- extension/package.json | 5 +- extension/scripts/coverIntegrationTests.js | 12 ++--- extension/scripts/virtualenv-install.js | 13 ----- extension/src/extension.ts | 4 +- extension/src/process/execution.test.ts | 32 ++++++++++++ extension/src/process/execution.ts | 3 +- extension/src/python/index.test.ts | 1 - extension/src/python/index.ts | 2 - extension/src/test/suite/cli/child.ts | 6 +-- .../src/test/suite/process/execution.test.ts | 36 ------------- extension/src/util/esm.ts | 29 ----------- package.json | 2 +- renovate.json | 2 +- scripts/virtualenv-install.ts | 14 ++++++ yarn.lock | 50 +++++++++---------- 15 files changed, 85 insertions(+), 126 deletions(-) delete mode 100644 extension/scripts/virtualenv-install.js create mode 100644 extension/src/process/execution.test.ts delete mode 100644 extension/src/test/suite/process/execution.test.ts delete mode 100644 extension/src/util/esm.ts create mode 100644 scripts/virtualenv-install.ts diff --git a/extension/package.json b/extension/package.json index 5c30da8e11..9de8252b04 100644 --- a/extension/package.json +++ b/extension/package.json @@ -1628,7 +1628,6 @@ "test-vscode": "node ./dist/test/runTest.js", "test-e2e": "wdio run ./src/test/e2e/wdio.conf.ts", "test": "jest --collect-coverage", - "setup-venv": "node ./scripts/virtualenv-install.js", "cover-vscode-run": "node ./scripts/coverIntegrationTests.js", "vscode:prepublish": "" }, @@ -1636,7 +1635,7 @@ "@hediet/std": "0.6.0", "@vscode/extension-telemetry": "0.7.7", "appdirs": "1.1.0", - "execa": "7.1.1", + "execa": "5.1.1", "fs-extra": "11.1.1", "js-yaml": "4.1.0", "json5": "2.2.3", @@ -1646,7 +1645,7 @@ "lodash.isequal": "4.5.0", "lodash.merge": "4.6.2", "lodash.omit": "4.5.0", - "process-exists": "5.0.0", + "process-exists": "4.1.0", "tree-kill": "1.2.2", "uuid": "9.0.0", "vega-util": "1.17.2", diff --git a/extension/scripts/coverIntegrationTests.js b/extension/scripts/coverIntegrationTests.js index 7703fb5a33..d5fe8ed840 100644 --- a/extension/scripts/coverIntegrationTests.js +++ b/extension/scripts/coverIntegrationTests.js @@ -1,10 +1,6 @@ const { resolve, join } = require('path') const { writeFileSync } = require('fs-extra') - -const getExeca = async () => { - const { execa } = await import('execa') - return execa -} +const execa = require('execa') let activationEvents = [] let failed @@ -22,7 +18,7 @@ activationEvents = packageJson.activationEvents packageJson.activationEvents = ['onStartupFinished'] writeFileSync(packageJsonPath, JSON.stringify(packageJson)) -getExeca().then(async execa => { +const runCover = async () => { const tests = execa('node', [join(cwd, 'dist', 'test', 'runTest.js')], { cwd }) @@ -43,4 +39,6 @@ getExeca().then(async execa => { if (failed) { process.exit(1) } -}) +} + +runCover() diff --git a/extension/scripts/virtualenv-install.js b/extension/scripts/virtualenv-install.js deleted file mode 100644 index a3399329e0..0000000000 --- a/extension/scripts/virtualenv-install.js +++ /dev/null @@ -1,13 +0,0 @@ -const { join, resolve } = require('path') -require('../dist/vscode/mockModule') - -const importModuleAfterMockingVsCode = async () => { - const { setupTestVenv } = require('../dist/python') - return setupTestVenv -} - -importModuleAfterMockingVsCode().then(setupTestVenv => { - const cwd = resolve(__dirname, '..', '..', 'demo') - - setupTestVenv(cwd, '.env', '-r', join('.', 'requirements.txt')) -}) diff --git a/extension/src/extension.ts b/extension/src/extension.ts index 7229f6320d..66bc19f6c8 100644 --- a/extension/src/extension.ts +++ b/extension/src/extension.ts @@ -50,7 +50,6 @@ import { DvcViewer } from './cli/dvc/viewer' import { registerSetupCommands } from './setup/register' import { Status } from './status' import { registerPersistenceCommands } from './persistence/register' -import { esmPackagesImported } from './util/esm' class Extension extends Disposable { protected readonly internalCommands: InternalCommands @@ -304,8 +303,7 @@ class Extension extends Disposable { let extension: undefined | Extension -export async function activate(context: ExtensionContext): Promise { - await esmPackagesImported +export function activate(context: ExtensionContext): void { extension = new Extension(context) context.subscriptions.push(extension) } diff --git a/extension/src/process/execution.test.ts b/extension/src/process/execution.test.ts new file mode 100644 index 0000000000..f8fb1e471d --- /dev/null +++ b/extension/src/process/execution.test.ts @@ -0,0 +1,32 @@ +import process from 'process' +import { executeProcess, processExists } from './execution' + +describe('executeProcess', () => { + it('should be able to run a process', async () => { + const output = await executeProcess({ + args: ['some', 'text'], + cwd: __dirname, + executable: 'echo' + }) + expect(output).toMatch(/some.*text/) + }) + + it('should return the stderr if the process throws with stderr', async () => { + await expect( + executeProcess({ + args: ['me', 'outside'], + cwd: __dirname, + executable: 'find' + }) + ).rejects.toBeTruthy() + }) +}) + +describe('processExists', () => { + it('should return true if the process exists', async () => { + expect(await processExists(process.pid)).toBe(true) + }) + it('should return false if it does not', async () => { + expect(await processExists(-123.321)).toBe(false) + }) +}) diff --git a/extension/src/process/execution.ts b/extension/src/process/execution.ts index ce6b4a7fa7..54c3d603da 100644 --- a/extension/src/process/execution.ts +++ b/extension/src/process/execution.ts @@ -2,9 +2,10 @@ import { ChildProcess } from 'child_process' import { Readable } from 'stream' import { Event, EventEmitter } from 'vscode' import { Disposable } from '@hediet/std/disposable' +import execa from 'execa' +import doesProcessExist from 'process-exists' import kill from 'tree-kill' import { getProcessPlatform } from '../env' -import { doesProcessExist, execa } from '../util/esm' interface RunningProcess extends ChildProcess { all?: Readable diff --git a/extension/src/python/index.test.ts b/extension/src/python/index.test.ts index 03e37df88b..c2563cb40a 100644 --- a/extension/src/python/index.test.ts +++ b/extension/src/python/index.test.ts @@ -5,7 +5,6 @@ import { getProcessPlatform } from '../env' jest.mock('../env') jest.mock('../process/execution') -jest.mock('../util/esm') const mockedGetProcessPlatform = jest.mocked(getProcessPlatform) const mockedCreateProcess = jest.mocked(createProcess) diff --git a/extension/src/python/index.ts b/extension/src/python/index.ts index 8e09dbfe89..86ecfa3b83 100644 --- a/extension/src/python/index.ts +++ b/extension/src/python/index.ts @@ -4,7 +4,6 @@ import { getProcessPlatform } from '../env' import { exists } from '../fileSystem' import { Logger } from '../common/logger' import { createProcess, executeProcess, Process } from '../process/execution' -import { esmPackagesImported } from '../util/esm' const sendOutput = (process: Process) => { process.all?.on('data', chunk => @@ -35,7 +34,6 @@ export const setupTestVenv = async ( envDir: string, ...installArgs: string[] ) => { - await esmPackagesImported if (!exists(join(cwd, envDir))) { const initVenv = createProcess({ args: ['-m', 'venv', envDir], diff --git a/extension/src/test/suite/cli/child.ts b/extension/src/test/suite/cli/child.ts index 6867cb91b3..757d159ae8 100644 --- a/extension/src/test/suite/cli/child.ts +++ b/extension/src/test/suite/cli/child.ts @@ -4,15 +4,13 @@ import { delay } from '../../../util/time' require('../../../vscode/mockModule') -const importModuleAfterMockingVsCode = async () => { +const importModuleAfterMockingVsCode = () => { const { Cli } = require('../../../cli') - const { esmPackagesImported } = require('../../../util/esm') - await esmPackagesImported return { Cli } } const main = async () => { - const { Cli } = await importModuleAfterMockingVsCode() + const { Cli } = importModuleAfterMockingVsCode() const cli = new Cli() diff --git a/extension/src/test/suite/process/execution.test.ts b/extension/src/test/suite/process/execution.test.ts deleted file mode 100644 index ae9a00e3e2..0000000000 --- a/extension/src/test/suite/process/execution.test.ts +++ /dev/null @@ -1,36 +0,0 @@ -import process from 'process' -import { describe, it, suite } from 'mocha' -import { expect } from 'chai' -import { executeProcess, processExists } from '../../../process/execution' - -suite('Process Manager Test Suite', () => { - describe('executeProcess', () => { - it('should be able to run a process', async () => { - const output = await executeProcess({ - args: ['some', 'text'], - cwd: __dirname, - executable: 'echo' - }) - expect(output).to.match(/some.*text/) - }) - - it('should return the stderr if the process throws with stderr', async () => { - await expect( - executeProcess({ - args: ['me', 'outside'], - cwd: __dirname, - executable: 'find' - }) - ).to.be.eventually.rejected - }) - }) - - describe('processExists', () => { - it('should return true if the process exists', async () => { - expect(await processExists(process.pid)).to.be.true - }) - it('should return false if it does not', async () => { - expect(await processExists(-123.321)).to.be.false - }) - }) -}) diff --git a/extension/src/util/esm.ts b/extension/src/util/esm.ts deleted file mode 100644 index 012ad90a5f..0000000000 --- a/extension/src/util/esm.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { Deferred } from '@hediet/std/synchronization' - -const deferred = new Deferred() -export const esmPackagesImported = deferred.promise - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore -type EsmExeca = typeof import('execa').execa -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore -type EsmProcessExists = typeof import('process-exists').processExists - -const shouldImportEsm = !process.env.JEST_WORKER_ID - -let execa: EsmExeca -let doesProcessExist: EsmProcessExists -const importEsmPackages = async () => { - const [{ execa: esmExeca }, { processExists: esmProcessExists }] = - await Promise.all([import('execa'), import('process-exists')]) - execa = esmExeca - doesProcessExist = esmProcessExists - deferred.resolve() -} - -if (shouldImportEsm) { - void importEsmPackages() -} - -export { execa, doesProcessExist } diff --git a/package.json b/package.json index b5badc564c..39c34a6d28 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "postinstall": "husky install && git submodule init && git submodule update && yarn svgr", "storybook": "yarn workspace dvc-vscode-webview storybook", "build-storybook": "yarn turbo run build-storybook --filter=dvc-vscode-webview", - "setup:venv": "yarn turbo run lint:build && yarn workspace dvc run setup-venv", + "setup:venv": "ts-node ./scripts/virtualenv-install.ts", "scheduled:cli:test": "ts-node ./extension/src/test/cli/index.ts", "create-svgs": "ts-node ./scripts/create-svgs.ts", "svgr": "yarn workspace dvc-vscode-webview svgr" diff --git a/renovate.json b/renovate.json index d70aa3c0f5..4e6cb10b27 100644 --- a/renovate.json +++ b/renovate.json @@ -1,5 +1,5 @@ { - "ignoreDeps": ["@types/node", "@types/vscode"], + "ignoreDeps": ["@types/node", "@types/vscode", "execa", "process-exists"], "extends": ["config:base"], "packageRules": [ { diff --git a/scripts/virtualenv-install.ts b/scripts/virtualenv-install.ts new file mode 100644 index 0000000000..e756cd683f --- /dev/null +++ b/scripts/virtualenv-install.ts @@ -0,0 +1,14 @@ +import { join, resolve } from 'path' +require('dvc/src/vscode/mockModule') + +const importModuleAfterMockingVsCode = () => { + const { setupTestVenv } = require('dvc/src/python') + + return setupTestVenv +} + +const setupTestVenv = importModuleAfterMockingVsCode() + +const cwd = resolve(__dirname, '..', 'demo') + +setupTestVenv(cwd, '.env', '-r', join('.', 'requirements.txt')) diff --git a/yarn.lock b/yarn.lock index 8014f97edf..ebee0dc4b8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10667,22 +10667,7 @@ evp_bytestokey@^1.0.0, evp_bytestokey@^1.0.3: md5.js "^1.3.4" safe-buffer "^5.1.1" -execa@7.1.1, execa@^7.1.1: - version "7.1.1" - resolved "https://registry.yarnpkg.com/execa/-/execa-7.1.1.tgz#3eb3c83d239488e7b409d48e8813b76bb55c9c43" - integrity sha512-wH0eMf/UXckdUYnO21+HDztteVv05rq2GXksxT4fCGeHkBhw1DROXh40wcjMcRqDOWE7iPJ4n3M7e2+YFP+76Q== - dependencies: - cross-spawn "^7.0.3" - get-stream "^6.0.1" - human-signals "^4.3.0" - is-stream "^3.0.0" - merge-stream "^2.0.0" - npm-run-path "^5.1.0" - onetime "^6.0.0" - signal-exit "^3.0.7" - strip-final-newline "^3.0.0" - -execa@^5.0.0, execa@^5.1.1: +execa@5.1.1, execa@^5.0.0, execa@^5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd" integrity sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg== @@ -10712,6 +10697,21 @@ execa@^7.0.0: signal-exit "^3.0.7" strip-final-newline "^3.0.0" +execa@^7.1.1: + version "7.1.1" + resolved "https://registry.yarnpkg.com/execa/-/execa-7.1.1.tgz#3eb3c83d239488e7b409d48e8813b76bb55c9c43" + integrity sha512-wH0eMf/UXckdUYnO21+HDztteVv05rq2GXksxT4fCGeHkBhw1DROXh40wcjMcRqDOWE7iPJ4n3M7e2+YFP+76Q== + dependencies: + cross-spawn "^7.0.3" + get-stream "^6.0.1" + human-signals "^4.3.0" + is-stream "^3.0.0" + merge-stream "^2.0.0" + npm-run-path "^5.1.0" + onetime "^6.0.0" + signal-exit "^3.0.7" + strip-final-newline "^3.0.0" + exenv-es6@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/exenv-es6/-/exenv-es6-1.1.1.tgz#80b7a8c5af24d53331f755bac07e84abb1f6de67" @@ -16550,12 +16550,12 @@ prismjs@^1.27.0, prismjs@~1.27.0: resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.27.0.tgz#bb6ee3138a0b438a3653dd4d6ce0cc6510a45057" integrity sha512-t13BGPUlFDR7wRB5kQDG4jjl7XeuH6jbJGt11JHPL96qwsEHNX2+68tFXqc1/k+/jALsbSWJKUOT/hcYAZ5LkA== -process-exists@5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/process-exists/-/process-exists-5.0.0.tgz#0b6dcd3d19e85e1f72c633f56d38e498196e2855" - integrity sha512-6QPRh5fyHD8MaXr4GYML8K/YY0Sq5dKHGIOrAKS3cYpHQdmygFCcijIu1dVoNKAZ0TWAMoeh8KDK9dF8auBkJA== +process-exists@4.1.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/process-exists/-/process-exists-4.1.0.tgz#4132c516324c1da72d65896851cdbd8bbdf5b9d8" + integrity sha512-BBJoiorUKoP2AuM5q/yKwIfT1YWRHsaxjW+Ayu9erLhqKOfnXzzVVML0XTYoQZuI1YvcWKmc1dh06DEy4+KzfA== dependencies: - ps-list "^8.0.0" + ps-list "^6.3.0" process-nextick-args@~2.0.0: version "2.0.1" @@ -16652,10 +16652,10 @@ prr@~1.0.1: resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476" integrity sha1-0/wRS6BplaRexok/SEzrHXj19HY= -ps-list@^8.0.0: - version "8.1.1" - resolved "https://registry.yarnpkg.com/ps-list/-/ps-list-8.1.1.tgz#9ff1952b26a9a07fcc05270407e60544237ae581" - integrity sha512-OPS9kEJYVmiO48u/B9qneqhkMvgCxT+Tm28VCEJpheTpl8cJ0ffZRRNgS5mrQRTrX5yRTpaJ+hRDeefXYmmorQ== +ps-list@^6.3.0: + version "6.3.0" + resolved "https://registry.yarnpkg.com/ps-list/-/ps-list-6.3.0.tgz#a2b775c2db7d547a28fbaa3a05e4c281771259be" + integrity sha512-qau0czUSB0fzSlBOQt0bo+I2v6R+xiQdj78e1BR/Qjfl5OHWJ/urXi8+ilw1eHe+5hSeDI1wrwVTgDp2wst4oA== pseudomap@^1.0.2: version "1.0.2"