tinesoft / nxrocks

Set of Nx plugins to enhance your Nx workspace (even more!)
MIT License
307 stars 34 forks source link

[Bug] nx-flutter: inferred tasks aren't working #227

Closed danielesegato closed 1 month ago

danielesegato commented 3 months ago

Plugin Name

create-nx-flutter

Nx Report

Node : 20.2.0 OS : darwin-arm64 npm : 9.6.6

nx (global) : 19.2.0 nx : 19.2.0 @nx/js : 19.2.0 @nx/workspace : 19.2.0 @nx/devkit : 19.2.0 @nrwl/tao : 19.2.0 typescript : 5.4.5

Registered Plugins: @nxrocks/nx-flutter

Community plugins: @nxrocks/nx-flutter : 9.0.2

Expected Behaviour

running

nx run-many -t test

should run tests on all flutter modules

Actual Behaviour

this is what happens

 NX   Successfully ran target test for 0 projects (20ms)

despite having projects in the workspace.

Steps to reproduce the behaviour

# create the workspace
npx create-nx-workspace@latest

# enter the workspace
cd the-workspace

# install nx-flutter plugin
npm install @nxrocks/nx-flutter --save-dev

# create a flutter project
npx nx g @nxrocks/nx-flutter:project 

# try running tests or any other command supported by nx-flutter
nx run-many -t test
tinesoft commented 3 months ago

Hi @danielesegato

Thanks for using the plugin and for reporting this.

Can you please share me the content of your .env file (if present at root) and your nx.json + project.json files please?

danielesegato commented 3 months ago

Sure.

there's no .env file at the moment, it's a new project - mostly empty.

The nx.json in the root of the workspace:

{
  "extends": "nx/presets/npm.json",
  "$schema": "./node_modules/nx/schemas/nx-schema.json",
  "plugins": [
    {
      "plugin": "@nxrocks/nx-flutter",
      "options": {
        "analyzeTargetName": "analyze",
        "cleanTargetName": "clean",
        "formatTargetName": "format",
        "testTargetName": "test",
        "doctorTargetName": "doctor",
        "assembleTargetName": "assemble",
        "attachTargetName": "attach",
        "driveTargetName": "drive",
        "genL10nTargetName": "gen-l10n",
        "installTargetName": "install",
        "runTargetName": "run",
        "buildAarTargetName": "build-aar",
        "buildApkTargetName": "build-apk",
        "buildAppbundleTargetName": "build-appbundle",
        "buildBundleTargetName": "build-bundle",
        "buildIosTargetName": "build-ios",
        "buildIosFrameworkTargetName": "build-ios-framework",
        "buildIpaTargetName": "build-ipa"
      }
    }
  ],
  "targetDefaults": {}
}

And a project.info file in a module at apps/poc relative directory

{
  "name": "apps/poc",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "apps/poc/src",
  "tags": []
}

this is what was generated by the plugin, I just changed the name of the project to match the folder

tinesoft commented 3 months ago

Hi,

can you try changing the name to simply poc in project.json ?

I'm not sure "apps/poc" is valid. What matters to find the files is the "sourceRoot".

danielesegato commented 3 months ago

I've created a new project to test this out.

Run this command from the route:

npx nx g @nxrocks/nx-flutter:project

and chose these options:

✔ What name would you like to use? · foo
✔ What package name would you like to use? · foo
✔ What is the project about? · Testing NX-Flutter
✔ Which type of Flutter project would you like to create? · app
✔ Are you using Flutter Version Manager (fvm)? (y/N) · true
✔ What should be the project name and where should it be generated? · foo @ foo
✔ Which platforms would you like to use? · android, ios
✔ Which Android language would you like to use? · kotlin
✔ Which iOS language would you like to use? · swift
⚙️ Generating project configuration...
Generating Flutter project with following options : --project-name=foo --org=foo --description="Testing NX-Flutter" --android-language=kotlin --ios-language=swift --template=app --platforms="android,ios" ...
Executing command: fvm flutter create --project-name=foo --org=foo --description="Testing NX-Flutter" --android-language=kotlin --ios-language=swift --template=app --platforms="android,ios"  foo
Creating project foo...
Resolving dependencies in `foo`... 
Downloading packages... 
Got dependencies in `foo`.
Wrote 74 files.

All done!

still getting 0 projects run when trying to run a target.

I've also tried renaming the project just "poc" but that didn't help either.

tinesoft commented 3 months ago

Hi @danielesegato

Thanks for your response. I will have a closer look this weekend and let you know.

Stay tuned

tinesoft commented 3 months ago

Hi @danielesegato

Sorry for the delay.

This should be fixed now in the latest version of the create-nx-flutter.

If you've already generated your workspace, you can simply update the version of @nxrocks/nx-flutter package in the nx.json to latest version (i.e 9.0.3) and run any nx command ( for example ./nx reset && ./nx graph)

If the version of package, in the nx.json is set to latest (default setting), simply delete the node_modules folder within the .nx/installation folder. Then run any nx command ( for example ./nx reset && ./nx graph).

Either action should force the download of the right version of underlying @nxrocks/nx-flutter plugin, that contains the fix.

Enjoy!

danielesegato commented 3 months ago

Thanks @tinesoft yes I can confirm some of the tasks are now running.

I did npm upgrade to update to the latest version.

Is it expected for build-aar to not run in flutter modules? (it only tried to run on the app module which doesn't have android libraries)

(is there a way to specify if I want to use FVM or not via environmental flag? it would be useful to disable FVM on CI)

tinesoft commented 3 months ago

Thanks for the updates.

Is it expected for build-aar to not run in flutter modules? (it only tried to run on the app module which doesn't have android libraries)

build-aar is only available when building for android platform.

(is there a way to specify if I want to use FVM or not via environmental flag? it would be useful to disable FVM on CI)

Right now, no. fvm is automatically used (instead of regular flutter) when it is found on the path. This is not configurable at the moment, but I agree, this can/should be configurable.

In the meantime, if you don't have fvm on your CI/CD, but just regular flutter binary, it should be good already

danielesegato commented 3 months ago

Hi @tinesoft I'm sorry, there's still some target not working:

npx nx run-many -t gen-l10n

produces:

 NX   Successfully ran target gen-l10n for 0 projects (15ms)

the same happen with, for example, the assemble target.

tinesoft commented 3 months ago

Hi @danielesegato

Inferred tasks depends on the type of project (template) your have generated, the target platforms you've chosen , etc...

The related code can be found here https://github.com/tinesoft/nxrocks/blob/0e2ae881800f72b450b5a1d5fb4e4491fcee50f8/packages/nx-flutter/src/utils/plugin-utils.ts#L68

Some of the values, are read at runtime from the .metadata file that is present at the root of each flutter project.

Can you show me the content of those files ?

danielesegato commented 3 months ago

core/auth .metadata

# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
  revision: "a14f74ff3a1cbd521163c5f03d68113d50af93d3"
  channel: "stable"

project_type: package

and apps/poc .metadata:

# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
  revision: "a14f74ff3a1cbd521163c5f03d68113d50af93d3"
  channel: "stable"

project_type: app

# Tracks metadata for the flutter migrate command
migration:
  platforms:
    - platform: root
      create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
      base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
    - platform: android
      create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
      base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
    - platform: ios
      create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
      base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
    - platform: linux
      create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
      base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
    - platform: macos
      create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
      base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
    - platform: web
      create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
      base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
    - platform: windows
      create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
      base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3

  # User provided section

  # List of Local paths (relative to this file) that should be
  # ignored by the migrate tool.
  #
  # Files that are not part of the templates will be ignored by default.
  unmanaged_files:
    - 'lib/main.dart'
    - 'ios/Runner.xcodeproj/project.pbxproj'

both of them contains localizations and should be able to run the gen-l10n task.

and I'd expect all projects to be able to run the assemble task.

I see the code checks for this app template, not sure what's the logic behind that.

tinesoft commented 3 months ago

Hi,

You might be right about the gen-l10n and assemble tasks. I 'll review the conditions for all tasks.

As for the poc project (which is of template app), I'm surprised it was not picked when you run npx nx run-many -t gen-l10n... Can you try running npx nx gen-l10n poc directly ?

danielesegato commented 3 months ago

Here it is @tinesoft

$ npx nx gen-l10n apps/poc --verbose

 NX   Cannot find configuration for task apps/poc:gen-l10n

Error: Cannot find configuration for task apps/poc:gen-l10n
    at ProcessTasks.createTask (/Volumes/sourcecode/[redacted]/node_modules/nx/src/tasks-runner/create-task-graph.js:156:19)
    at ProcessTasks.processTasks (/Volumes/sourcecode/[redacted]/node_modules/nx/src/tasks-runner/create-task-graph.js:23:39)
    at createTaskGraph (/Volumes/sourcecode/[redacted]/node_modules/nx/src/tasks-runner/create-task-graph.js:194:21)
    at createTaskGraphAndValidateCycles (/Volumes/sourcecode/[redacted]/node_modules/nx/src/tasks-runner/run-command.js:62:63)
    at /Volumes/sourcecode/[redacted]/node_modules/nx/src/tasks-runner/run-command.js:85:27
    at handleErrors (/Volumes/sourcecode/[redacted]/node_modules/nx/src/utils/params.js:9:30)
    at runCommand (/Volumes/sourcecode/[redacted]/node_modules/nx/src/tasks-runner/run-command.js:83:52)
    at Object.runOne (/Volumes/sourcecode/[redacted]/node_modules/nx/src/command-line/run/run-one.js:53:59)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async handleErrors (/Volumes/sourcecode/[redacted]/node_modules/nx/src/utils/params.js:9:24)

regarding the gen-l10n command it should only be available on projects and modules that have generation anabled and the l10n.yaml file I believe.

for the assemble one I'm not sure.

denny99 commented 2 months ago

Hm. For me it is still not working. I've tried several commands, but it always states: Cannot find configuration.

I'm using the latest 9.1.1 version of this plugin.

I did the same as the author, as i had an existing workspace and just wanted to add the flutter subproject.

# install nx-flutter plugin
npm install @nxrocks/nx-flutter --save-dev

# create a flutter project
npx nx g @nxrocks/nx-flutter:project 

Funny side note:

I've just named the project flutter-app. This caused a lot of trouble with flutter as the name included a -. So i had to recreate it with an underscore (flutter_app)

tinesoft commented 2 months ago

Hi @denny99 ,

Can you make sure that you're using at least version v9.1.2: https://github.com/tinesoft/nxrocks/releases/tag/nx-flutter%2Fv9.1.2?

Try removing node_modules, then reinstalling them.

As for this:

I've just named the project flutter-app. This caused a lot of trouble with flutter as the name included a -. So i had to recreate it with an underscore (flutter_app)

can you open another ticket, with reproduction steps?

denny99 commented 2 months ago
image

Nope sorry. Still the same issue.

Edit:

The naming issue seems to be fixed. I used an older version that caused the issue

tinesoft commented 2 months ago

Hum,

the issue seems specific to your setup....

can you provide more information about your workspace? how you created it? the files/folders structure?

better yet, if you can share a minimum reproductible project, that would be great

denny99 commented 2 months ago
npx create-nx-workspace@18.3.5

 NX   Let's create a new workspace [https://nx.dev/getting-started/intro]

✔ Where would you like to create your workspace? · flutter-repro
✔ Which stack do you want to use? · none
✔ Package-based monorepo, integrated monorepo, or standalone project? · integrated
✔ Set up CI with caching, distribution and test deflaking · skip
✔ Would you like remote caching to make your build faster? · skip

cd flutter-repro/

npm i -D @nxrocks/nx-flutter

nx g @nxrocks/nx-flutter:project flutter-app

 NX  Generating @nxrocks/nx-flutter:project

✔ What package name would you like to use? · com.example
✔ What is the project about? ·
✔ Which type of Flutter project would you like to create? · app
✔ Are you using Flutter Version Manager (fvm)? (y/N) · false
✔ Which platforms would you like to use? · android, ios
✔ Which Android language would you like to use? · kotlin
✔ Which iOS language would you like to use? · swift

nx clean flutter-app

 NX   Cannot find configuration for task flutter-app:clean

Pass --verbose to see the stacktrace.

It is basically the same as the author described it. It does not matter if i use nx 18 oder 19

tinesoft commented 2 months ago

Ok thanks for the complementary information. I'll try to have another look at today.

Stay tuned!

tinesoft commented 2 months ago

I just tried your exact steps, and could not reproduce the issue...

image

Can you show me the content of your nx.json, project.json and .env(if present). ?

Also, regarding your previous response image This error is normal, because the flutter project name is "flutter_app" (note the underscore, so that the name matches Dart naming convention), whereas the nx project name is the one you provided ("flutter-app"). So you must use the later to run nx commands on it.

denny99 commented 1 month ago

Then i'm kinda confused. I've tried pnpm as well but it does not make any difference. My Flutter Version is: Flutter 3.22.3

And here are my nx and project json files nx.json

{
  "$schema": "./node_modules/nx/schemas/nx-schema.json",
  "namedInputs": {
    "default": [
      "{projectRoot}/**/*",
      "sharedGlobals"
    ],
    "production": [
      "default"
    ],
    "sharedGlobals": []
  },
  "plugins": [
    {
      "plugin": "@nxrocks/nx-flutter",
      "options": {
        "analyzeTargetName": "analyze",
        "cleanTargetName": "clean",
        "formatTargetName": "format",
        "testTargetName": "test",
        "assembleTargetName": "assemble",
        "attachTargetName": "attach",
        "driveTargetName": "drive",
        "genL10nTargetName": "gen-l10n",
        "installTargetName": "install",
        "runTargetName": "run",
        "buildAarTargetName": "build-aar",
        "buildApkTargetName": "build-apk",
        "buildAppbundleTargetName": "build-appbundle",
        "buildBundleTargetName": "build-bundle",
        "buildIosTargetName": "build-ios",
        "buildIosFrameworkTargetName": "build-ios-framework",
        "buildIpaTargetName": "build-ipa"
      }
    }
  ],
  "targetDefaults": {}
}

project.json

{
  "name": "flutter-app",
  "$schema": "../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "flutter-app/src",
  "tags": []
}

The flutter_app was my renamed project. So nx and flutter package names are identical. Just for testing purposes i've used flutter-app for nx (which will result in a flutter_app in the pubspec file)

tinesoft commented 1 month ago

Can you try clearing Nx cache with nx reset and retry the command?

If it still doesn"t work, I'm afraid I cannot assist you more without accessing your code, or at least a minimal repro project showing the issue...

denny99 commented 1 month ago

Here you go

https://github.com/denny99/nxrocks

pnpm i or npm i. Does not matter.

nx clean flutter-app will result in no target found.

The NX UI in Webstorm does not show any targets either

image
tinesoft commented 1 month ago

Thanks for the repro project, it will help a lot finding and fixing the issue. I'll have a look tonight, stay tuned!

tinesoft commented 1 month ago

OK, the problem is now fixed.

Simply update the plugin to latest version (v9.2.0) and you should be good. Tested on your repro project above.

denny99 commented 1 month ago

Thanks awesome!