Closed MarekLew closed 7 years ago
How to open a file in a workspace with a command line?
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 |
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 |
From command line open file code a.txt
I don't believe that opens a workspace but just the file. cc @bpasero to confirm
From command line open file code a.txt
I don't believe that opens a workspace but just the file. cc @bpasero to confirm
mkdir tmp
cd tmp
code .
echo xxx>a.txt
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).
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
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.
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).
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
.
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];
}
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?
I use the same code to test...
Structure:
D:\Develop\Test\
.vscode\
src\
file1.txt
file2.txt
Here a video to illustrate :
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.
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.
So to have the right case, a temporary workaround could be: let wsFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(file.fsPath));
Wow, that is interesting. Thanks for the investigation!
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
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.
Extensions: the listing length exceeds browsers' URL characters limit
Steps to Reproduce:
local-history
extensioncode .
)echo xxx>a.txt
code a.txt
.history
folderA debug extension and in
history.serring.js:15
functiongetWorkspaceFolder
return None
, when file never open fromexplorer bar
.If file open form command line then
getWorkspaceFolder
return None
. If file open explorer(VSC) thengetWorkspaceFolder
return workspace folderReproduces without extensions: No