Skip to content

Commit

Permalink
Use workspace folders as cwd for remote notebooks (#1931)
Browse files Browse the repository at this point in the history
* Use workspace folders as cwd for remote notebooks

* Words

---------

Co-authored-by: Sebastian (Tiedtke) Huckleberry <[email protected]>
  • Loading branch information
pastuxso and sourishkrout authored Jan 28, 2025
1 parent 36bccc3 commit 17a6459
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,10 @@
{
"view": "runme.launcher",
"contents": "Open a workspace/folder to display Runme files here"
},
{
"view": "runme.remoteNotebooks",
"contents": "Explore Cloud Notebooks"
}
],
"keybindings": [
Expand Down
16 changes: 15 additions & 1 deletion src/extension/executors/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import fs from 'node:fs/promises'
import os from 'node:os'

import {
NotebookCellOutput,
Expand All @@ -10,13 +11,16 @@ import {
NotebookCellData,
Uri,
NotebookDocument,
workspace,
WorkspaceFolder,
} from 'vscode'

import { DEFAULT_PROMPT_ENV, OutputType, RUNME_FRONTMATTER_PARSED } from '../../constants'
import type { CellOutputPayload, Serializer, ShellType } from '../../types'
import { NotebookCellOutputManager } from '../cell'
import { getAnnotations, getWorkspaceFolder } from '../utils'
import { CommandMode, CommandModeEnum } from '../grpc/runner/types'
import { RunmeFsScheme } from '../provider/runmeFs'

const HASH_PREFIX_REGEXP = /^\s*\#\s*/g
const ENV_VAR_REGEXP = /(\$\w+)/g
Expand Down Expand Up @@ -240,7 +244,17 @@ export async function getCellCwd(
// TODO: support windows here
(notebook?.metadata as Serializer.Metadata | undefined)?.[RUNME_FRONTMATTER_PARSED]?.cwd,
getAnnotations(cell.metadata as Serializer.Metadata | undefined).cwd,
]
].filter(Boolean)

if (notebook && 'uri' in notebook && notebook.uri.scheme === RunmeFsScheme) {
const folders: readonly WorkspaceFolder[] = workspace.workspaceFolders || []
if (folders.length > 0) {
candidates.push(...folders.map((f) => f.uri.fsPath))
} else {
const fallbackCwd = await fs.mkdtemp(path.join(os.tmpdir(), 'runme-fallback-cwd-'))
candidates.push(fallbackCwd)
}
}

for (let candidate of candidates) {
if (!candidate) {
Expand Down
4 changes: 2 additions & 2 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ import { RunmeIdentity } from './grpc/parser/tcp/types'
import * as features from './features'
import AuthSessionChangeHandler from './authSessionChangeHandler'
import { CloudNotebooks } from './provider/cloudNotebooks'
import RunmeFS from './provider/runmeFs'
import RunmeFS, { RunmeFsScheme } from './provider/runmeFs'

export class RunmeExtension {
protected serializer?: ISerializer
Expand All @@ -109,7 +109,7 @@ export class RunmeExtension {
await ContextState.addKey(FeatureName.RemoteNotebooks, true)
const runmeFs = new RunmeFS()
context.subscriptions.push(
workspace.registerFileSystemProvider('runmefs', runmeFs, {
workspace.registerFileSystemProvider(RunmeFsScheme, runmeFs, {
isReadonly: false,
}),
commands.registerCommand('runme.addRemoteNotebooks', (_) => {
Expand Down
6 changes: 4 additions & 2 deletions src/extension/provider/runmeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const excludedPaths = [
'.runme_bootstrap_demo',
]

export const RunmeFsScheme = 'runmefs'

export function mergeUriPaths(uri: Uri, newPath: string): Uri {
const isAbsolutePath = newPath.startsWith('/')

Expand All @@ -54,11 +56,11 @@ export default class RunmeFS implements FileSystemProvider {
#pathTree: PathTree = {}

get root() {
return Uri.parse('runmefs:///')
return Uri.parse(`${RunmeFsScheme}:///`)
}

static resolveUri(path: string) {
return Uri.parse(`runmefs:///${path}`)
return Uri.parse(`${RunmeFsScheme}:///${path}`)
}

async readFile(uri: Uri): Promise<Uint8Array> {
Expand Down

0 comments on commit 17a6459

Please sign in to comment.