Skip to content

Commit

Permalink
Merge pull request #23498 from Social-Mean/issue-23497
Browse files Browse the repository at this point in the history
PR: Fix `--connect-to-kernel` cli option when a project is loaded at startup
  • Loading branch information
ccordoba12 authored Jan 30, 2025
2 parents b60ea8f + a04098f commit 66d9f6a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 24 deletions.
17 changes: 12 additions & 5 deletions spyder/plugins/ipythonconsole/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,16 +560,23 @@ def on_close(self, cancelable=False):

def on_mainwindow_visible(self):
"""
Connect to an existing kernel if a `kernel-*.json` file is given via
Connect to an existing kernel if a `kernel-*.json` file is given via
command line options. Otherwise create a new client.
"""
cli_options = self.get_command_line_options()
connection_file = cli_options.connection_file

if connection_file is not None:
self.create_client_for_kernel(
self.get_widget().find_connection_file(connection_file),
give_focus=False,
)
cf_path = self.get_widget().find_connection_file(connection_file)
if cf_path is None:
# Show an error if the connection file passed on the command
# line doesn't exist (find_connection_file returns None in that
# case).
self.create_new_client(give_focus=False)
client = self.get_current_client()
client.show_kernel_connection_error()
else:
self.create_client_for_kernel(cf_path, give_focus=False)
else:
self.create_new_client(give_focus=False)

Expand Down
10 changes: 10 additions & 0 deletions spyder/plugins/ipythonconsole/widgets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,16 @@ def show_kernel_error(self, error):
# Stop shellwidget
self.shellwidget.shutdown()

def show_kernel_connection_error(self):
self.show_kernel_error(
_(
"It was not possible to connect to the kernel associated to "
"this console. If you are trying to connect to an existing "
"kernel, check that the connection file you selected actually "
"corresponds to the kernel you want to connect to."
)
)

def is_benign_error(self, error):
"""Decide if an error is benign in order to filter it."""
benign_errors = [
Expand Down
18 changes: 14 additions & 4 deletions spyder/plugins/ipythonconsole/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,14 +1350,24 @@ def find_connection_file(self, connection_file):
"""Fix connection file path."""
cf_path = osp.dirname(connection_file)
cf_filename = osp.basename(connection_file)

# To change a possible empty string to None
cf_path = cf_path if cf_path else None
connection_file = find_connection_file(filename=cf_filename,
path=cf_path)
if os.path.splitext(connection_file)[1] != ".json":

# This error is raised when find_connection_file can't find the file
try:
connection_file = find_connection_file(
filename=cf_filename, path=cf_path
)
except OSError:
connection_file = None

if connection_file and os.path.splitext(connection_file)[1] != ".json":
# There might be a file with the same id in the path.
connection_file = find_connection_file(
filename=cf_filename + ".json", path=cf_path)
filename=cf_filename + ".json", path=cf_path
)

return connection_file

# ---- Public API
Expand Down
9 changes: 1 addition & 8 deletions spyder/plugins/ipythonconsole/widgets/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -1409,14 +1409,7 @@ def _kernel_restarted_message(self, died=True):
+ self.kernel_handler.fault_filename()
)
else:
self.ipyclient.show_kernel_error(
_(
"It was not possible to connect to the kernel. If you "
"are trying to connect to an existing kernel, check "
"that the connection file you selected actually "
"corresponds to the kernel you want to connect to."
)
)
self.ipyclient.show_kernel_connection_error()

self._append_html(f"<br>{msg}<br>", before_prompt=False)
self.insert_horizontal_ruler()
Expand Down
13 changes: 11 additions & 2 deletions spyder/plugins/projects/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,28 @@ def on_mainwindow_visible(self):
cli_options = self.get_command_line_options()
initial_cwd = self._main.get_initial_working_directory()

# There's no need to restart the console if the user wants to connect
# to a kernel at startup.
# Fixes spyder-ide/spyder#23497
restart_console = cli_options.connection_file is None

if cli_options.project is not None:
logger.debug('Opening project from the command line')
project = osp.normpath(
osp.join(initial_cwd, cli_options.project)
)
self.open_project(
project,
workdir=cli_options.working_directory
workdir=cli_options.working_directory,
restart_console=restart_console
)
else:
self.get_widget().set_pane_empty()
logger.debug('Reopening project from last session')
self.get_widget().reopen_last_project()
self.get_widget().reopen_last_project(
working_directory=cli_options.working_directory,
restart_console=restart_console
)

# ---- Public API
# -------------------------------------------------------------------------
Expand Down
9 changes: 4 additions & 5 deletions spyder/plugins/projects/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,9 @@ def change_max_recent_projects(self):
self.recent_projects = self._get_valid_recent_projects(
self.recent_projects)[:max_projects]

def reopen_last_project(self):
def reopen_last_project(self, working_directory, restart_console):
"""
Reopen the active project when Spyder was closed last time, if any
Reopen the active project when Spyder was closed last time, if any.
"""
current_project_path = self.get_conf('current_project_path',
default=None)
Expand All @@ -530,12 +530,11 @@ def reopen_last_project(self):
current_project_path and
self.is_valid_project(current_project_path)
):
cli_options = self.get_plugin().get_command_line_options()
self.open_project(
path=current_project_path,
restart_console=True,
restart_console=restart_console,
save_previous_files=False,
workdir=cli_options.working_directory
workdir=working_directory,
)
self._load_config()

Expand Down

0 comments on commit 66d9f6a

Please sign in to comment.