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

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

Closed
MarekLew opened this issue Oct 13, 2017 · 17 comments
Assignees
Labels
api bug Issue identified by VS Code Team member as probable bug verified Verification succeeded workbench-multiroot Multi-root (multiple folders) issues
Milestone

Comments

@MarekLew
Copy link

MarekLew commented Oct 13, 2017

  • VSCode Version: Code 1.17.1 (1e9d365, 2017-10-10T14:24:41.632Z)
  • OS Version: Windows_NT ia32 10.0.15063
  • Extensions: the listing length exceeds browsers' URL characters limit

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
Copy link
Author

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

@sandy081 sandy081 assigned jrieken and unassigned sandy081 Oct 13, 2017
@MarekLew MarekLew changed the title It's VSC bug vscode.workspace.getWorkspaceFolder(file) don't return workspace folder when file open from command line. vscode.workspace.getWorkspaceFolder(file) don't return workspace folder when file open from command line. Oct 13, 2017
@MarekLew
Copy link
Author

On linux I don`t have this problem:

  • VSCode Version: Code 1.17.1 (1e9d365, 2017-10-10T14:21:18.489Z)
  • OS Version: Linux x64 4.10.0-37-generic
  • Extensions:
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
Copy link
Author

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.

  • VSCode Version: Code 1.17.1 (1e9d365, 2017-10-10T14:24:41.632Z)
  • OS Version: Windows_NT ia32 10.0.14393
  • Extensions:
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 jrieken added info-needed Issue requires more information from poster and removed new release labels Oct 16, 2017
@jrieken
Copy link
Member

jrieken commented Oct 16, 2017

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
Copy link
Author

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
  1. open VSC in folder -- open workspace
    code .
  2. create file -- create file in workspace
    echo xxx>a.txt
  3. 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
Copy link
Member

jrieken commented Oct 16, 2017

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
Copy link
Author

MarekLew commented Oct 16, 2017

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
Copy link
Author

MarekLew commented Oct 17, 2017

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
Copy link
Member

bpasero commented Oct 17, 2017

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
Copy link

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
Copy link
Member

jrieken commented Oct 18, 2017

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
Copy link

zabel-xyz commented Oct 18, 2017

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
Copy link
Author

MarekLew commented Oct 18, 2017

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:

  • /c:/Users/marek/Desktop/vsc_test/a.txt

path for file b.txt opended from command line:

  • /C:/Users/marek/Desktop/vsc_test/b.txt

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
Copy link

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

@jrieken
Copy link
Member

jrieken commented Oct 23, 2017

Wow, that is interesting. Thanks for the investigation!

@jrieken jrieken added bug Issue identified by VS Code Team member as probable bug workbench-multiroot Multi-root (multiple folders) issues api and removed info-needed Issue requires more information from poster workbench labels Oct 23, 2017
@jrieken jrieken added this to the October 2017 milestone Oct 23, 2017
@jrieken
Copy link
Member

jrieken commented Oct 23, 2017

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

@jrieken jrieken closed this as completed Oct 23, 2017
@mjbvz mjbvz added the verified Verification succeeded label Nov 3, 2017
@MarekLew
Copy link
Author

MarekLew commented Nov 11, 2017

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.

@vscodebot vscodebot bot locked and limited conversation to collaborators Dec 7, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api bug Issue identified by VS Code Team member as probable bug verified Verification succeeded workbench-multiroot Multi-root (multiple folders) issues
Projects
None yet
Development

No branches or pull requests

6 participants