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

Disallow creating projects with an app that starts with a number #2127

Open
rmartin16 opened this issue Jan 21, 2025 · 5 comments
Open

Disallow creating projects with an app that starts with a number #2127

rmartin16 opened this issue Jan 21, 2025 · 5 comments
Labels
bug A crash or error in behavior. good first issue Is this your first time contributing? This could be a good place to start!

Comments

@rmartin16
Copy link
Member

Describe the bug

It is possible to create projects with an app that starts with a number but this is not valid.

❯ briefcase new -Q app_name=1hello --no-input && cd 1hello && briefcase dev

Using override value '1hello'

[1hello] Generating a new application 'Hello World'
Using app template: https://github.com/beeware/briefcase-template, branch v0.3.21
Template branch v0.3.21 not found; falling back to development template
Using existing template (sha af397117020084a0c284c65132c0e6c1019c1b65, updated Tue Dec 31 08:20:07 2024)

[1hello] Generated new application 'Hello World'

To run your application, type:

    $ cd 1hello
    $ briefcase dev


[1hello] Installing requirements...
Requirement already satisfied: toga-gtk~=0.4.7 in /home/russell/.pyenv/versions/3.10.15/envs/briefcase-3.10/lib/python3.10/site-packages (0.4.9.dev300+g1b7609dd8)
Requirement already satisfied: pytest in /home/russell/.pyenv/versions/3.10.15/envs/briefcase-3.10/lib/python3.10/site-packages (8.3.4)
Requirement already satisfied: pycairo>=1.17.0 in /home/russell/.pyenv/versions/3.10.15/envs/briefcase-3.10/lib/python3.10/site-packages (from toga-gtk~=0.4.7) (1.27.0)
Requirement already satisfied: pygobject>=3.50.0 in /home/russell/.pyenv/versions/3.10.15/envs/briefcase-3.10/lib/python3.10/site-packages (from toga-gtk~=0.4.7) (3.50.0)
Requirement already satisfied: toga-core==0.4.9.dev300+g1b7609dd8 in /home/russell/.pyenv/versions/3.10.15/envs/briefcase-3.10/lib/python3.10/site-packages (from toga-gtk~=0.4.7) (0.4.9.dev300+g1b7609dd8)
Requirement already satisfied: travertino>=0.3.0 in /home/russell/.pyenv/versions/3.10.15/envs/briefcase-3.10/lib/python3.10/site-packages (from toga-core==0.4.9.dev300+g1b7609dd8->toga-gtk~=0.4.7) (0.3.0)
Requirement already satisfied: exceptiongroup>=1.0.0rc8 in /home/russell/.pyenv/versions/3.10.15/envs/briefcase-3.10/lib/python3.10/site-packages (from pytest) (1.2.2)
Requirement already satisfied: iniconfig in /home/russell/.pyenv/versions/3.10.15/envs/briefcase-3.10/lib/python3.10/site-packages (from pytest) (2.0.0)
Requirement already satisfied: packaging in /home/russell/.pyenv/versions/3.10.15/envs/briefcase-3.10/lib/python3.10/site-packages (from pytest) (24.2)
Requirement already satisfied: pluggy<2,>=1.5 in /home/russell/.pyenv/versions/3.10.15/envs/briefcase-3.10/lib/python3.10/site-packages (from pytest) (1.5.0)
Requirement already satisfied: tomli>=1 in /home/russell/.pyenv/versions/3.10.15/envs/briefcase-3.10/lib/python3.10/site-packages (from pytest) (2.2.1)
Installing dev requirements... done

[1hello] Starting in dev mode...
===========================================================================
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/russell/.pyenv/versions/3.10.15/lib/python3.10/runpy.py", line 220, in run_module
    mod_name, mod_spec, code = _get_module_details(mod_name)
  File "/home/russell/.pyenv/versions/3.10.15/lib/python3.10/runpy.py", line 146, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/home/russell/.pyenv/versions/3.10.15/lib/python3.10/runpy.py", line 157, in _get_module_details
    code = loader.get_code(mod_name)
  File "<frozen importlib._bootstrap_external>", line 1017, in get_code
  File "<frozen importlib._bootstrap_external>", line 947, in source_to_code
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/russell/tmp/beeware/1hello/src/1hello/__main__.py", line 1
    from 1hello.app import main
         ^
SyntaxError: invalid decimal literal

Problem running app 1hello.

Steps to reproduce

Run briefcase new -Q app_name=1hello --no-input && cd 1hello && briefcase dev

Expected behavior

App names that begin with a number should not be allowed.

Screenshots

No response

Environment

  • Operating System: pop os 22.04
  • Python version: 3.10
  • Software versions:
    • Briefcase: 0.3.21.dev125+gdced62d4

Logs

briefcase.2025_01_21-00_06_20.dev.log

Additional context

No response

@rmartin16 rmartin16 added the bug A crash or error in behavior. label Jan 21, 2025
@freakboy3742
Copy link
Member

... huh.

I'm more than a little surprised by this one, honestly. I didn't know that PEP 508 allowed specifying a module name that isn't valid Python module... I guess it makes sense, though, as there's no hard requirement that the module installed by a PyPI package matches the name of the PyPI package.

@freakboy3742 freakboy3742 added the good first issue Is this your first time contributing? This could be a good place to start! label Jan 21, 2025
@manognya-b
Copy link

Can I take up this issue? Will send out updates on progress and blockers if any!

@freakboy3742
Copy link
Member

@manognya-b Absolutely! We don't formally assign bugs, so feel free to submit a fix (and an update for the existing test!)

@manognya-b
Copy link

manognya-b commented Jan 25, 2025

Hello again, I've disallowed app names that begin with numbers and it is working as expected and rejecting both single and multiple character strings that begin with numbers. However, I noticed a phenomenon where app names that contain a "." character are being parsed incorrectly during the briefcase dev run.

@manognya-b ➜ /workspaces/briefcase (2127.bugfix.invalid-app-name) $ briefcase new -Q app_name=hello.1 --no-input && cd hello.1 && briefcase d
ev

Using override value 'hello.1'

[hello.1] Generating a new application 'Hello World'
Using app template: https://github.com/beeware/briefcase-template, branch v0.1
Template branch v0.1 not found; falling back to development template
Using existing template (sha af397117020084a0c284c65132c0e6c1019c1b65, updated Tue Dec 31 08:20:07 2024)

[hello.1] Generated new application 'Hello World'

To run your application, type:

    $ cd hello.1
    $ briefcase dev


Briefcase configuration error: Configuration for 'hello' is incomplete (missing 'description', 'sources')

As seen above, the app name hello.1 is being truncated to hello. A temporary fix for this would be to disallow . usages in app names, but as per PEP508 compliance rules, usage of a . as long it isn't the beginning or ending of the string seems legal.

Reference: https://peps.python.org/pep-0508/#:~:text=%5E(%5BA%2DZ0%2D9%5D%7C%5BA%2DZ0%2D9%5D%5BA%2DZ0%2D9._%2D%5D*%5BA%2DZ0%2D9%5D)%24

(Something interesting: the reference regex is allowing numbers to be used at the beginning of strings?)

Would this be a separate issue as it could be related to how briefcase is parsing app names? Thanks!

@freakboy3742
Copy link
Member

Hello again, I've disallowed app names that begin with numbers and it is working as expected and rejecting both single and multiple character strings that begin with numbers. However, I noticed a phenomenon where app names that contain a "." character are being parsed incorrectly during the briefcase dev run.

Nice catch! I can confirm that's the behaviour I'm seeing as well.

As seen above, the app name hello.1 is being truncated to hello. A temporary fix for this would be to disallow . usages in app names, but as per PEP508 compliance rules, usage of a . as long it isn't the beginning or ending of the string seems legal.

I think this is pointing at the root of the problem - PEP508 compliance is clearly the wrong rule to be applying here. There are plenty of names that are valid PyPI package names, but have no meaningful way to be used as module names.

It looks like we actually need a strict subset of PEP 508 - identifiers that are valid PEP 508, but can also be converted into a valid identifier. Thankfully, the latter can be validated using str.isdentifier() in the standard library. We also need to allow for the hello-world case (because that's a valid PEP 508 name, but is converted by Briefcase into hello_world as a module name; so validating candidate.replace('-','_').isidentifier() should be sufficient. That should completely remove then need to check PEP 508, because any identifier that satisifies that check would also be PEP 508 compliant.

(of course, we then also need to update all the places in the documentation and help strings where we say "PEP 508" is the rule for app names...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A crash or error in behavior. good first issue Is this your first time contributing? This could be a good place to start!
Projects
None yet
Development

No branches or pull requests

3 participants