Skip to content

Do not execute shebang as an interpreter until user has clicked on the codelens enclosing the shebang #11816

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

Merged
merged 4 commits into from
May 18, 2020
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
1 change: 1 addition & 0 deletions news/2 Fixes/11687.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not execute shebang as an interpreter until user has clicked on the codelens enclosing the shebang.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export class SetShebangInterpreterCommand extends BaseInterpreterSelectorCommand

protected async setShebangInterpreter(): Promise<void> {
const shebang = await this.shebangCodeLensProvider.detectShebang(
this.documentManager.activeTextEditor!.document
this.documentManager.activeTextEditor!.document,
true
);
if (!shebang) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/client/interpreter/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export interface IInterpreterDisplay {

export const IShebangCodeLensProvider = Symbol('IShebangCodeLensProvider');
export interface IShebangCodeLensProvider extends CodeLensProvider {
detectShebang(document: TextDocument): Promise<string | undefined>;
detectShebang(document: TextDocument, resolveShebangAsInterpreter?: boolean): Promise<string | undefined>;
}

export const IInterpreterHelper = Symbol('IInterpreterHelper');
Expand Down
13 changes: 10 additions & 3 deletions src/client/interpreter/display/shebangCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export class ShebangCodeLensProvider implements IShebangCodeLensProvider {
// tslint:disable-next-line:no-any
this.onDidChangeCodeLenses = (workspaceService.onDidChangeConfiguration as any) as Event<void>;
}
public async detectShebang(document: TextDocument): Promise<string | undefined> {
public async detectShebang(
document: TextDocument,
resolveShebangAsInterpreter: boolean = false
): Promise<string | undefined> {
const firstLine = document.lineAt(0);
if (firstLine.isEmptyOrWhitespace) {
return;
Expand All @@ -30,8 +33,12 @@ export class ShebangCodeLensProvider implements IShebangCodeLensProvider {
}

const shebang = firstLine.text.substr(2).trim();
const pythonPath = await this.getFullyQualifiedPathToInterpreter(shebang, document.uri);
return typeof pythonPath === 'string' && pythonPath.length > 0 ? pythonPath : undefined;
if (resolveShebangAsInterpreter) {
const pythonPath = await this.getFullyQualifiedPathToInterpreter(shebang, document.uri);
return typeof pythonPath === 'string' && pythonPath.length > 0 ? pythonPath : undefined;
} else {
return typeof shebang === 'string' && shebang.length > 0 ? shebang : undefined;
}
}
public async provideCodeLenses(document: TextDocument, _token?: CancellationToken): Promise<CodeLens[]> {
return this.createShebangCodeLens(document);
Expand Down
30 changes: 24 additions & 6 deletions src/test/providers/shebangCodeLenseProvider.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,33 @@ suite('Shebang detection', () => {

return [doc, line];
}
test('Shebang should be empty when first line is empty', async () => {
test('Shebang should be empty when first line is empty when resolving shebang as interpreter', async () => {
const [document, line] = createDocument('');

const shebang = await provider.detectShebang(document.object);
const shebang = await provider.detectShebang(document.object, true);

document.verifyAll();
line.verifyAll();
expect(shebang).to.be.equal(undefined, 'Shebang should be undefined');
});
test('Shebang should be empty when first line is empty when not resolving shebang as interpreter', async () => {
const [document, line] = createDocument('');

const shebang = await provider.detectShebang(document.object, false);

document.verifyAll();
line.verifyAll();
expect(shebang).to.be.equal(undefined, 'Shebang should be undefined');
});
test('Shebang should be returned as it is when not resolving shebang as interpreter', async () => {
const [document, line] = createDocument('#!HELLO');

const shebang = await provider.detectShebang(document.object, false);

document.verifyAll();
line.verifyAll();
expect(shebang).to.be.equal('HELLO', 'Shebang should be HELLO');
});
test('Shebang should be empty when python path is invalid in shebang', async () => {
const [document, line] = createDocument('#!HELLO');

Expand All @@ -80,7 +98,7 @@ suite('Shebang detection', () => {
.returns(() => Promise.reject())
.verifiable(typemoq.Times.once());

const shebang = await provider.detectShebang(document.object);
const shebang = await provider.detectShebang(document.object, true);

document.verifyAll();
line.verifyAll();
Expand All @@ -95,7 +113,7 @@ suite('Shebang detection', () => {
.returns(() => Promise.resolve({ stdout: 'THIS_IS_IT' }))
.verifiable(typemoq.Times.once());

const shebang = await provider.detectShebang(document.object);
const shebang = await provider.detectShebang(document.object, true);

document.verifyAll();
line.verifyAll();
Expand All @@ -113,7 +131,7 @@ suite('Shebang detection', () => {
.returns(() => Promise.resolve({ stdout: 'THIS_IS_IT' }))
.verifiable(typemoq.Times.once());

const shebang = await provider.detectShebang(document.object);
const shebang = await provider.detectShebang(document.object, true);

document.verifyAll();
line.verifyAll();
Expand All @@ -132,7 +150,7 @@ suite('Shebang detection', () => {
.returns(() => Promise.resolve({ stdout: 'THIS_IS_IT' }))
.verifiable(typemoq.Times.once());

const shebang = await provider.detectShebang(document.object);
const shebang = await provider.detectShebang(document.object, true);

document.verifyAll();
line.verifyAll();
Expand Down