// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. 'use strict'; import * as path from 'path'; import { IWorkspaceService } from '../common/application/types'; import { IConfigurationService } from '../common/types'; import { IDataScienceFileSystem } from './types'; export async function calculateWorkingDirectory( configService: IConfigurationService, workspace: IWorkspaceService, fs: IDataScienceFileSystem ): Promise { let workingDir: string | undefined; // For a local launch calculate the working directory that we should switch into const settings = configService.getSettings(undefined); const fileRoot = settings.datascience.notebookFileRoot; // If we don't have a workspace open the notebookFileRoot seems to often have a random location in it (we use ${workspaceRoot} as default) // so only do this setting if we actually have a valid workspace open if (fileRoot && workspace.hasWorkspaceFolders) { const workspaceFolderPath = workspace.workspaceFolders![0].uri.fsPath; if (path.isAbsolute(fileRoot)) { if (await fs.localDirectoryExists(fileRoot)) { // User setting is absolute and exists, use it workingDir = fileRoot; } else { // User setting is absolute and doesn't exist, use workspace workingDir = workspaceFolderPath; } } else if (!fileRoot.includes('${')) { // fileRoot is a relative path, combine it with the workspace folder const combinedPath = path.join(workspaceFolderPath, fileRoot); if (await fs.localDirectoryExists(combinedPath)) { // combined path exists, use it workingDir = combinedPath; } else { // Combined path doesn't exist, use workspace workingDir = workspaceFolderPath; } } else { // fileRoot is a variable that hasn't been expanded workingDir = fileRoot; } } return workingDir; }