diff --git a/PyInstaller/__main__.py b/PyInstaller/__main__.py index c096b60954..b8fc0f3493 100644 --- a/PyInstaller/__main__.py +++ b/PyInstaller/__main__.py @@ -197,6 +197,15 @@ def run(pyi_args: list | None = None, pyi_config: dict | None = None): ) spec_file = args.filenames[0] else: + # Ensure that the given script files exist, before trying to generate the .spec file. + # This prevents us from overwriting an existing (and customized) .spec file if user makes a typo in the + # .spec file's suffix when trying to build it, for example, `pyinstaller program.cpes` (see #8276). + # It also prevents creation of a .spec file when `pyinstaller program.py` is accidentally ran from a + # directory that does not contain the script (for example, due to failing to change the directory prior + # to running the command). + for filename in args.filenames: + if not os.path.isfile(filename): + raise SystemExit(f"Script file {filename!r} does not exist.") spec_file = run_makespec(**vars(args)) sys.argv = [spec_file, *spec_args] diff --git a/news/8279.bugfix.rst b/news/8279.bugfix.rst new file mode 100644 index 0000000000..193afe3baf --- /dev/null +++ b/news/8279.bugfix.rst @@ -0,0 +1,5 @@ +When trying to run ``pyinstaller`` (or equivalent ``python -m PyInstaller``) +against non-existing script file(s), exit immediately - without trying +to write the .spec file and building it. This prevents us from overwriting +an existing (and customized) .spec file if user makes a typo in the .spec +file's suffix when trying to build it, for example, ``pyinstaller program.cpes``.