microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.09k stars 29.26k forks source link

`vscode.workspace.getWorkspaceFolder(file)` don't return workspace folder when file open from command line. #36221

Closed MarekLew closed 7 years ago

MarekLew commented 7 years ago

Steps to Reproduce:

  1. install local-history extension
  2. open VSC in folder (code .)
  3. create file echo xxx>a.txt
  4. From command line open file code a.txt
  5. Change and save file.
  6. Extension don't create file in .history folder

A debug extension and in history.serring.js:15 function getWorkspaceFolder return None, when file never open from explorer bar.

If file open form command line then getWorkspaceFolder return None. If file open explorer(VSC) then getWorkspaceFolder return workspace folder

Reproduces without extensions: No

MarekLew commented 7 years ago

How to open a file in a workspace with a command line?

MarekLew commented 7 years ago

On linux I don`t have this problem:

Extension Author (truncated) Version
vsc-material-theme Equ 1.0.5
Bookmarks ale 0.16.0
project-manager ale 0.21.1
code-gnu-global aus 0.2.2
spellright ban 1.2.18
python don 0.7.0
LogFileHighlighter emi 1.2.0
git-project-manager fel 1.4.0
MagicPython mag 1.0.12
quicksnippet mou 0.0.1
hg mrc 1.1.7
cpptools ms- 0.13.1
annotator ryu 0.10.1
guides spy 0.8.4
python tht 0.2.3
gitflow vec 1.1.1
local-history xyz 1.6.0
MarekLew commented 7 years ago

On another computer I update extension and run in Code 1.16.1 then getWorkspaceFolder work corectly. After update to Code 1.17.1 getWorkspaceFolder nothing returned.

Extension Author (truncated) Version
Bookmarks ale 0.16.0
project-manager ale 0.21.1
githistory don 0.2.3
python don 0.7.0
MagicPython mag 1.0.12
vscode-fileheader mik 0.0.2
hg mrc 1.1.7
cpptools ms- 0.13.1
vscode-icons rob 7.15.0
gerrit-vscode tht 0.4.1
python tht 0.2.3
local-history xyz 1.6.0
jrieken commented 7 years ago

From command line open file code a.txt

I don't believe that opens a workspace but just the file. cc @bpasero to confirm

MarekLew commented 7 years ago

From command line open file code a.txt

I don't believe that opens a workspace but just the file. cc @bpasero to confirm

  1. Create folder
    mkdir tmp
    cd tmp
  2. open VSC in folder -- open workspace code .
  3. create file -- create file in workspace echo xxx>a.txt
  4. From command line open file -- open file existing in workspace code a.txt or code -r a.txt

And then getWorkspaceFolder does not recognize that the file exists in the workspace (probably only on windows).

jrieken commented 7 years ago

open VSC in folder -- open workspace

That isn't what we call a workspace, but a folder. A workspace is one or many folders that are being opened and a workspace is stored a .code-workspace file. Also, note that workspaces aren't available in stable but only in insiders

MarekLew commented 7 years ago

Thank you for your interest

vscode.workspace.getWorkspaceFolder work fine for single folder(not workspace) on Code 1.16.1 and Code 1.18.0-Insider but not for Code 1.17.1 (all for windows 10) and it must work in future for single folder(not Workspace) because vscode.workspace.rootPath is now deprecated.

The problem no longer exists in version 1.18.0-instider. I believe it will be in the final version.

NOTE: It's only problem for file open from outside VSC (command line and also opened from Windows explorer). For file opened from visual studio code panel it`s work fine.

MarekLew commented 7 years ago

Also, note that workspaces aren't available in stable but only in insiders

It's not full available but Code 1.17.1 create workspace(drag and drop folders) and open .code-workspace files from command line as workspace(not as a single file but group of folders).

bpasero commented 7 years ago

From command line open file code a.txt

I don't believe that opens a workspace but just the file. cc @bpasero to confirm

Yes, a workspace file will only be opened if the extension is .code-workspace.

zabel-xyz commented 7 years ago

Like @MarekLew say, this bug is only present in code 1.17.1 (In 1.18.0-Insider, it works fine)

I use vscode.workspace.getWorkspaceFolder(uri) to prepare Multi-root workspace implementation, so I eliminate rootPath, according to doc

With this bug, I'll add temporary a piece of code like that:

// Find workspaceFolder corresponding to file
let wsFolder = vscode.workspace.getWorkspaceFolder(file);
// Temporary code to resolve bug:
// vscode.workspace.getWorkspaceFolder(file) return null when file is opened from command line. 
// Even if the right folder is already open in vscode (e.g. --reuse window)
if (!wsFolder && file && vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length === 1) {
    if (file.fsPath.includes(vscode.workspace.workspaceFolders[0].uri.fsPath))
        wsFolder = vscode.workspace.workspaceFolders[0];
    }
jrieken commented 7 years ago

Still not sure about this. I have an extension that this and it behaves the same on master/insiders and 1.17.2 which is: print undefined when just a file has been opened and print the folder when a folder (no workspace) has been opened.

    vscode.window.onDidChangeActiveTextEditor(e => {
        if (e) {
            const folder = vscode.workspace.getWorkspaceFolder(e.document.uri);
            console.log(folder);
        }
    })

Maybe this is an issue about how/when a folder is getting opened even though you specified a file on the command line?

zabel-xyz commented 7 years ago

I use the same code to test...

Structure:

    D:\Develop\Test\
       .vscode\
       src\
          file1.txt
          file2.txt
  1. Open folder Test in vscode
  2. Open each files in a tab, vscode.workspace.getWorkspaceFolder() return "D:\Develop\Test"
  3. Close tab file2.txt
  4. Keep vscode open and run this command in a shell
    call code --reuse-window --goto D:\Develop\Test\src\file2.txt:5:2
  5. file2.txt is reopen in tab2, but vscode.workspace.getWorkspaceFolder() return undefined this time (file1.txt still return "D:\Develop\Test")

Here a video to illustrate : getworkspacefolder

I quite often use vscode through a command line, to switch from another program (ide+poor editor) to vscode (a great editor). So I can edit the same file at the same position with vscode.

MarekLew commented 7 years ago

I found problem ( tested only for code 1.17.2 windows 10)

File object(vscode.Uri) have attribute path but

path for file a.txt opended from VSC:

path for file b.txt opended from command line:

Uppercase letter of the disk!

It's not only problem for file opened from comand line. File create in VSC CTRL+N and saved, have the same problem.

zabel-xyz commented 7 years ago

So to have the right case, a temporary workaround could be: let wsFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(file.fsPath));

jrieken commented 7 years ago

Wow, that is interesting. Thanks for the investigation!

jrieken commented 7 years ago

I have pushed a test to ensure this behaviour is correct. Closing we don't ship 1.17.3 because of this (multi root support will only become "official" with 1.18). Thanks again

MarekLew commented 6 years ago

On finally visual studio code 1.18 (windows 10) vscode.workspace.getWorkspaceFolder return correct object but Uppercase letter of the disk exist in URI.path for file loaded from outside VSC. It's probably not a bug but exist.