databricks-vscode icon indicating copy to clipboard operation
databricks-vscode copied to clipboard

[BUG] Notebook userName() not overridden in dbutils

Open TumbleTuesday opened this issue 1 year ago • 3 comments

Describe the bug notebook.entry_point.getDbutils().notebook().getContext().userName().get() does not get overrriden when used through Databricks Connect / VS Code Extension. This causes the python file to error out.

To Reproduce Steps to reproduce the behavior:

Insert notebook.entry_point.getDbutils().notebook().getContext().userName().get() into a .py file and attempt to run (after implementing spark and package imports etc.)
See error

Additional context

notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()

does get overriden however in [databricks.sdk.dbutils.py]([http://databricks.sdk.dbutils.py/]](http://databricks.sdk.dbutils.py/%5D) so can this same method be done for the userName() call?

proxy_override_paths = { 'notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()': get_local_notebook_path, 'notebook.entry_point.getDbutils().notebook().getContext().userName().get()': get_local_notebook_path, }

TumbleTuesday avatar Mar 10 '25 23:03 TumbleTuesday

I think we can reliably implement this only for code executed directly with "Run/Debug current file with Databricks Connect" commands. Meaning that it won't work for interactive notebook execution, as there we don't control how the execution starts, and so can't inject necessary env vars. There are ways to implement it for notebooks by reading in-project env files which are updated by the extension, but it might now work for all edge cases.

Can you consider using non-dbutils way of obtaining the user? See this example, which should work both on a remote cluster and locally.

ilia-db avatar Mar 28 '25 10:03 ilia-db

Thanks for the response, the SDK workspace client does seem to be the better way of getting the current user. We use the dbutils method in a logging module we have created. I assume this would still work when it is run by ADF or Databricks Workflows?

I did also create an identical issue on the Databricks Python SDK when I made this ticket to have it implemented natively if possible.

TumbleTuesday avatar Mar 28 '25 11:03 TumbleTuesday

Yes, it should work inside Databricks Workflows, as long as you are running on DBR 13.3 or higher. A bit more info is here - https://docs.databricks.com/aws/en/dev-tools/sdk-python#use-the-databricks-sdk-for-python-from-a-databricks-notebook

ilia-db avatar Mar 31 '25 08:03 ilia-db