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

Formatting a notebook results in having the last cell's code pasted into every cell. It also doesn't format the notebook. #16980

Closed
greazer opened this issue Aug 14, 2021 · 3 comments · Fixed by #17650
Labels
area-formatting bug Issue identified by VS Code Team member as probable bug important Issue identified as high-priority partner ask

Comments

@greazer
Copy link
Member

greazer commented Aug 14, 2021

Version: 1.60.0-insider (user setup)
Commit: 58454f7bec838f79fe2422ab5914419147d82e40
Date: 2021-08-13T05:15:29.421Z
Electron: 13.1.8
Chrome: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Windows_NT x64 10.0.22000

Python: v2021.9.1125171759-dev
Jupyter: v2021.8.2027072902
Pylance: v2021.8.2-pre.1

  1. Open a notebook. I used the one from https://github.com/greazer/covid-19-data/tree/master/SampleNbsAndScripts
  2. Right click on the first code cell and choose Format Notebook...

A lot of activity will occur (based on the progress bar spinning for a few seconds), but the only formatting that will have happened is that code from the last cell in the notebook will be pasted into the top of every cell.

Clearly this is wrong. :)
recording (5)

@greazer
Copy link
Member Author

greazer commented Aug 14, 2021

Direct assignment to @rebornix just cuz of how unexpected this behavior is. :)

@rebornix
Copy link
Member

We now request cell formatting in parallel (cc @joyceerhl) but it seems Python extension generates temp file for notebook cell formatting but it only uses uri.fsPath and doesn't include fragment, thus all notebook cells are using the same temp file for formatting.

export async function getTempFileWithDocumentContents(document: TextDocument, fs: IFileSystem): Promise<string> {
// Don't create file in temp folder since external utilities
// look into configuration files in the workspace and are not
// to find custom rules if file is saved in a random disk location.
// This means temp file has to be created in the same folder
// as the original one and then removed.
// Use a .tmp file extension (instead of the original extension)
// because the language server is watching the file system for Python
// file add/delete/change and we don't want this temp file to trigger it.
let fileName = `${document.uri.fsPath}.${md5(document.uri.fsPath)}.tmp`;
try {
// When dealing with untitled notebooks, there's no original physical file, hence create a temp file.
if (isNotebookCell(document.uri) && !(await fs.fileExists(document.uri.fsPath))) {
fileName = (await fs.createTemporaryFile(`${path.basename(document.uri.fsPath)}.tmp`)).filePath;
}
await fs.writeFile(fileName, document.getText());
} catch (ex) {
traceError('Failed to create a temporary file', ex);
throw new WrappedError(`Failed to create a temporary file, ${ex.message}`, ex);
}
return fileName;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-formatting bug Issue identified by VS Code Team member as probable bug important Issue identified as high-priority partner ask
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@rebornix @karthiknadig @greazer and others