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

Adds support for enabling WSL Mode for the shell language only #303

Merged
merged 2 commits into from
Dec 15, 2023
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
2 changes: 1 addition & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ esbuild.build({
logLevel: "info",
sourcemap: prod ? false : 'inline',
treeShaking: true,
outfile: 'src/main.js',
outfile: 'main.js',
}).catch(() => process.exit(1));
20 changes: 15 additions & 5 deletions src/executors/NonInteractiveCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import Executor from "./Executor";
import {Outputter} from "src/Outputter";
import {LanguageId} from "src/main";
import { ExecutorSettings } from "../settings/Settings.js";
import { sep } from "path";
import { join } from "path/posix";
import windowsPathToWsl from "../transforms/windowsPathToWsl.js";

export default class NonInteractiveCodeExecutor extends Executor {
Expand Down Expand Up @@ -38,13 +36,13 @@ export default class NonInteractiveCodeExecutor extends Executor {

fs.promises.writeFile(tempFileName, codeBlockContent).then(() => {
const args = cmdArgs ? cmdArgs.split(" ") : [];
if (this.settings.wslMode) {

if (this.isWSLEnabled()) {
args.unshift("-e", cmd);
cmd = "wsl";
args.push(windowsPathToWsl(tempFileName));
} else {
args.push(tempFileName);
args.push(tempFileName);
}

const child = child_process.spawn(cmd, args, {env: process.env, shell: this.usesShell});
Expand All @@ -63,6 +61,18 @@ export default class NonInteractiveCodeExecutor extends Executor {
});
}

private isWSLEnabled(): boolean {
if (this.settings.wslMode) {
return true;
}

if (this.language == 'shell' && this.settings.shellWSLMode) {
return true;
}

return false;
}

/**
* Handles the output of a child process and redirects stdout and stderr to the given {@link Outputter} element.
* Removes the temporary file after the code execution. Creates a new Notice after the code execution.
Expand Down
2 changes: 2 additions & 0 deletions src/settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ExecutorSettings {
timeout: number;
allowInput: boolean;
wslMode: boolean;
shellWSLMode: boolean;
onlyCurrentBlock: boolean;
nodePath: string;
nodeArgs: string;
Expand Down Expand Up @@ -175,6 +176,7 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
timeout: 10000,
allowInput: true,
wslMode: false,
shellWSLMode: false,
onlyCurrentBlock: false,
nodePath: "node",
nodeArgs: "",
Expand Down
14 changes: 13 additions & 1 deletion src/settings/per-lang/makeShellSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,17 @@ export default (tab: SettingsTab, containerEl: HTMLElement) => {
console.log('Shell file extension set to: ' + value);
await tab.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Shell WSL mode')
.setDesc('Run the shell script in Windows Subsystem for Linux. This option is used if the global "WSL Mode" is disabled.')
.addToggle((toggle) =>
toggle
.setValue(tab.plugin.settings.shellWSLMode)
.onChange(async (value) => {
tab.plugin.settings.shellWSLMode = value;
await tab.plugin.saveSettings();
})
);
tab.makeInjectSetting(containerEl, "shell");
}
}