odoo-ide / vscode-odoo

Visual Studio Code extension for Odoo
https://marketplace.visualstudio.com/items?itemName=trinhanhngoc.vscode-odoo
46 stars 3 forks source link

Granular control over import paths? #27

Closed AaronBeaudoin closed 1 year ago

AaronBeaudoin commented 1 year ago

Hello, I've been using VS Code for Odoo development for some time without your plugin. To get addon imports mostly working I've had my project structured like this:

_odoo/
  community/
    odoo/
      <odoo_source_code>
  enterprise/
    odoo/
      addons/
        <enterprise_addons>
addons/
  <my_custom_addons>

Then in my .vscode/settings.json I have this:

{
  "python.analysis.extraPaths": [
    "_odoo/community",
    "_odoo/enterprise"
  ]
}

As a result, any from odoo.addons.<whatever> import something works perfectly as long as <whatever> is a one of the built-in Odoo modules or an enterprise module. However, this only works because the paths I added to extraPaths are structured to contain odoo/addons. I don't want to structure my custom addons directory like that, and even if I did it wouldn't work because inside of it I organize my modules into subdirectories something like this because I a have a lot of them:

addons/
  company/
    <my_company_addons>
  partner/
    <company_partner_addons>
  vendor/
    <3rd_party_addons>

I also don't want to turn my project into a multi-root workspace—I prefer the simplicity of opening a single root directory in VS Code. Is there any way for me to add all of my custom subdirectories to import resolution manually?

trinhanhngoc commented 1 year ago

Hi @AaronBeaudoin . Unfortunately, creating namespace packages (odoo/addons) for your custom addons is the only way for odoo.addons.* imports to be understood by pyright/pylance. If you use the Odoo extension, you can make it work by configuring something like this:

"python.analysis.extraPaths": [
    "_odoo/community",
    "_odoo/enterprise",
    "addons/company",
    "addons/partner",
    "addons/vendor"
]
AaronBeaudoin commented 1 year ago

So when you say in the README:

Both the Odoo source code and your custom addons must be added to your VSCode workspace for the Odoo extension to work as expected. You can use the multi-root workspaces feature to structure your Odoo workspace.

I can do what you suggested above to avoid having to structure my project using multi-root workspaces?

trinhanhngoc commented 1 year ago

So when you say in the README:

Both the Odoo source code and your custom addons must be added to your VSCode workspace for the Odoo extension to work as expected. You can use the multi-root workspaces feature to structure your Odoo workspace.

I can do what you suggested above to avoid having to structure my project using multi-root workspaces?

Yes. I prefer the multi-root workspace method but your method works fine too.