Problems with --project-dir option to pio init

I’m experimenting because I’m working through the author review part of the second edition of Arduino Software Internals, and the PlatformIO chapter has been updated.

I note that there is a --project-dir or -d option to pio init. However, the operation is inconsitent.

In my first test, I didn’t create the output directory, and it failed, advising me that the output directory didn’t exist. I can cope with that, but it would be nice if it created the output as it creates the src, lib directories anyway, normally.

In my second test, I created the directory and specified it thus:

mkdir fred
pio init -b uno -d fred

That fell over with a lot of red text and Python errors—see below. However, it did create the usual directories and most of platformio.ini—the comments were there but the [uno] environment was missing.

I cleared out and tried again with a relative path.

rm -Rf fred
mkdir fred
pio init -b uno -d ./fred

Same problem, same messages, same output.

Finally, I tested with a full path:

rm -Rf fred
mkdir fred
pio init -b uno -d $PWD/fred

And this worked perfectly.

The output from the failing tests is:

The following files/directories have been created in fred
include - Put project header files here
lib - Put project specific (private) libraries here
src - Put project source files here
platformio.ini - Project Configuration File
FileNotFoundError: Traceback (most recent call last):
  File "/home/norman/.platformio/penv/lib/python3.10/site-packages/platformio/__main__.py", line 103, in main
    cli()  # pylint: disable=no-value-for-parameter
  File "/home/norman/.platformio/penv/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/home/norman/.platformio/penv/lib/python3.10/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/home/norman/.platformio/penv/lib/python3.10/site-packages/platformio/cli.py", line 85, in invoke
    return super().invoke(ctx)
  File "/home/norman/.platformio/penv/lib/python3.10/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/norman/.platformio/penv/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/norman/.platformio/penv/lib/python3.10/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/home/norman/.platformio/penv/lib/python3.10/site-packages/platformio/project/commands/init.py", line 92, in project_init_cmd
    update_board_envs(project_dir, boards, project_options, env_prefix)
  File "/home/norman/.platformio/penv/lib/python3.10/site-packages/platformio/project/commands/init.py", line 336, in update_board_envs
    config.save()
  File "/home/norman/.platformio/penv/lib/python3.10/site-packages/platformio/project/config.py", line 551, in save
    with open(path or self.path, mode="w+", encoding="utf8") as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'fred/platformio.ini'

============================================================

An unexpected error occurred. Further steps:

* Verify that you have the latest version of PlatformIO using
  `python -m pip install -U platformio` command

* Try to find answer in FAQ Troubleshooting section
  https://docs.platformio.org/page/faq/index.html

* Report this problem to the developers
  https://github.com/platformio/platformio-core/issues

============================================================

The bit about “FileNotFoundError: [Errno 2] No such file or directory: ‘fred/platformio.ini’” seems misleading as it managed to create the file, although I suspect that’s more of a “copy and paste” operation. I suspect it’s trying to open the file to write the environment settings. I’m not a Python programmer but it looks to me like it can’t tell the difference between a full path or a relative one, and I find that hard to believe.

Cheers,
Norm.

Solid grounds for a ticket in https://github.com/platformio/platformio-core/issues

1 Like

Thanks @maxgerhardt. I have logged this as Issue 4847.

Cheers,
Norm.

1 Like