Skip to content

Commit 1fb5920

Browse files
Kartik Rajkarthiknadig
Kartik Raj
andcommitted
Do not execute shebang as an interpreter until user has clicked on the codelens enclosing the shebang (microsoft#11816)
* Do not execute shebang as an interpreter until user has clicked on the codelens enclosing the shebang * Rename * Oops * Update src/test/providers/shebangCodeLenseProvider.unit.test.ts Co-authored-by: Karthik Nadig <[email protected]> Co-authored-by: Karthik Nadig <[email protected]>
1 parent d1b7b8e commit 1fb5920

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

news/2 Fixes/11687.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Do not execute shebang as an interpreter until user has clicked on the codelens enclosing the shebang.

src/client/interpreter/configuration/interpreterSelector/commands/setShebangInterpreter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export class SetShebangInterpreterCommand extends BaseInterpreterSelectorCommand
3737

3838
protected async setShebangInterpreter(): Promise<void> {
3939
const shebang = await this.shebangCodeLensProvider.detectShebang(
40-
this.documentManager.activeTextEditor!.document
40+
this.documentManager.activeTextEditor!.document,
41+
true
4142
);
4243
if (!shebang) {
4344
return;

src/client/interpreter/contracts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export interface IInterpreterDisplay {
114114

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

120120
export const IInterpreterHelper = Symbol('IInterpreterHelper');

src/client/interpreter/display/shebangCodeLensProvider.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ export class ShebangCodeLensProvider implements IShebangCodeLensProvider {
1919
// tslint:disable-next-line:no-any
2020
this.onDidChangeCodeLenses = (workspaceService.onDidChangeConfiguration as any) as Event<void>;
2121
}
22-
public async detectShebang(document: TextDocument): Promise<string | undefined> {
22+
public async detectShebang(
23+
document: TextDocument,
24+
resolveShebangAsInterpreter: boolean = false
25+
): Promise<string | undefined> {
2326
const firstLine = document.lineAt(0);
2427
if (firstLine.isEmptyOrWhitespace) {
2528
return;
@@ -30,8 +33,12 @@ export class ShebangCodeLensProvider implements IShebangCodeLensProvider {
3033
}
3134

3235
const shebang = firstLine.text.substr(2).trim();
33-
const pythonPath = await this.getFullyQualifiedPathToInterpreter(shebang, document.uri);
34-
return typeof pythonPath === 'string' && pythonPath.length > 0 ? pythonPath : undefined;
36+
if (resolveShebangAsInterpreter) {
37+
const pythonPath = await this.getFullyQualifiedPathToInterpreter(shebang, document.uri);
38+
return typeof pythonPath === 'string' && pythonPath.length > 0 ? pythonPath : undefined;
39+
} else {
40+
return typeof shebang === 'string' && shebang.length > 0 ? shebang : undefined;
41+
}
3542
}
3643
public async provideCodeLenses(document: TextDocument, _token?: CancellationToken): Promise<CodeLens[]> {
3744
return this.createShebangCodeLens(document);

src/test/providers/shebangCodeLenseProvider.unit.test.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,33 @@ suite('Shebang detection', () => {
6363

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

69-
const shebang = await provider.detectShebang(document.object);
69+
const shebang = await provider.detectShebang(document.object, true);
7070

7171
document.verifyAll();
7272
line.verifyAll();
7373
expect(shebang).to.be.equal(undefined, 'Shebang should be undefined');
7474
});
75+
test('Shebang should be empty when first line is empty when not resolving shebang as interpreter', async () => {
76+
const [document, line] = createDocument('');
77+
78+
const shebang = await provider.detectShebang(document.object, false);
79+
80+
document.verifyAll();
81+
line.verifyAll();
82+
expect(shebang).to.be.equal(undefined, 'Shebang should be undefined');
83+
});
84+
test('Shebang should be returned as it is when not resolving shebang as interpreter', async () => {
85+
const [document, line] = createDocument('#!HELLO');
86+
87+
const shebang = await provider.detectShebang(document.object, false);
88+
89+
document.verifyAll();
90+
line.verifyAll();
91+
expect(shebang).to.be.equal('HELLO', 'Shebang should be HELLO');
92+
});
7593
test('Shebang should be empty when python path is invalid in shebang', async () => {
7694
const [document, line] = createDocument('#!HELLO');
7795

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

83-
const shebang = await provider.detectShebang(document.object);
101+
const shebang = await provider.detectShebang(document.object, true);
84102

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

98-
const shebang = await provider.detectShebang(document.object);
116+
const shebang = await provider.detectShebang(document.object, true);
99117

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

116-
const shebang = await provider.detectShebang(document.object);
134+
const shebang = await provider.detectShebang(document.object, true);
117135

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

135-
const shebang = await provider.detectShebang(document.object);
153+
const shebang = await provider.detectShebang(document.object, true);
136154

137155
document.verifyAll();
138156
line.verifyAll();

0 commit comments

Comments
 (0)