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

dialogs - validate and normalize default path #186467

Merged
merged 2 commits into from
Jun 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/vs/workbench/contrib/files/browser/files.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ configurationRegistry.registerConfiguration({
},
'files.dialog.defaultPath': {
'type': 'string',
'pattern': '^((\\/|\\\\\\\\|[a-zA-Z]:\\\\).*)?$', // slash OR UNC-root OR drive-root OR undefined
'patternErrorMessage': nls.localize('defaultPathErrorMessage', "Default path for file dialogs must be an absolute path (e.g. C:\\\\myFolder or /myFolder)."),
'description': nls.localize('fileDialogDefaultPath', "Default path for file dialogs, overriding user's home path. Only used in the absence of a context-specific path, such as most recently opened file or folder."),
'scope': ConfigurationScope.MACHINE
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IHistoryService } from 'vs/workbench/services/history/common/history';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { URI } from 'vs/base/common/uri';
import * as resources from 'vs/base/common/resources';
import * as path from 'vs/base/common/path';
import { IInstantiationService, } from 'vs/platform/instantiation/common/instantiation';
import { ISimpleFileDialog, SimpleFileDialog } from 'vs/workbench/services/dialogs/browser/simpleFileDialog';
import { IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
Expand Down Expand Up @@ -93,17 +94,21 @@ export abstract class AbstractFileDialogService implements IFileDialogService {
}

async preferredHome(schemeFilter = this.getSchemeFilterForWindow()): Promise<URI> {

// Seek a user-local or user-remote machine-scoped override path string depending on whether caller wants a local or a remote home
const inspectedValue = this.configurationService.inspect<string>('files.dialog.defaultPath');
const dialogHomePath = schemeFilter === Schemas.file ? inspectedValue.userLocalValue : inspectedValue.userRemoteValue;
const userHomePromise = this.pathService.userHome({ preferLocal: schemeFilter === Schemas.file });
if (!dialogHomePath) {
return userHomePromise;
const preferLocal = schemeFilter === Schemas.file;
const preferredHomeConfig = this.configurationService.inspect<string>('files.dialog.defaultPath');
const preferredHomeCandidate = preferLocal ? preferredHomeConfig.userLocalValue : preferredHomeConfig.userRemoteValue;
if (preferredHomeCandidate) {
const pathLib = preferLocal ? path : await this.pathService.path;
if (pathLib.isAbsolute(preferredHomeCandidate)) {
const preferredHomeNormalized = pathLib.normalize(preferredHomeCandidate);
const preferredHome = resources.toLocalResource(await this.pathService.fileURI(preferredHomeNormalized), this.environmentService.remoteAuthority, this.pathService.defaultUriScheme);
if (await this.fileService.exists(preferredHome)) {
bpasero marked this conversation as resolved.
Show resolved Hide resolved
return preferredHome;
}
}
}

const resource = await this.pathService.fileURI(dialogHomePath);
return resources.toLocalResource(resource, this.environmentService.remoteAuthority, this.pathService.defaultUriScheme);
return this.pathService.userHome({ preferLocal });
}

async defaultWorkspacePath(schemeFilter = this.getSchemeFilterForWindow()): Promise<URI> {
Expand Down