q-shift / backstage-plugins

Apache License 2.0
3 stars 7 forks source link

Review instructions to link/unlink a module/package #36

Open cmoulliard opened 9 months ago

cmoulliard commented 9 months ago

TODO

Review instructions to link/unlnk a module/package within the DEVELOPER.md file

Producer of package

TO LINK
 ~/code/openshift/backstage-quarkus-plugins/plugins/quarkus-console 
yarn link
success Registered "@qshift/plugin-quarkus-console".

VERIFY
ls -la ~/.config/yarn/link/@qshift                
total 0
drwxr-xr-x@ 3 cmoullia  staff  96 Feb  9 06:51 .
drwxr-xr-x@ 3 cmoullia  staff  96 Jan 15 21:39 ..
lrwxr-xr-x@ 1 cmoullia  staff  76 Feb  9 06:51 plugin-quarkus-console -> ../../../../code/openshift/backstage-quarkus-plugins/plugins/quarkus-console

TO UNLINK
yarn unlink
success Unregistered "@qshift/plugin-quarkus-console".
info You can now run `yarn unlink "@qshift/plugin-quarkus-console"` in the projects where you no longer want to use this package.
✨  Done in 0.02s.

Consumer of the package

TO LINK
~/code/openshift/fork-backstage-playground

yarn link "@qshift/plugin-quarkus-console"
success Using linked package for "@qshift/plugin-quarkus-console".
✨  Done in 0.04s.

ls -la node_modules/@qshift/plugin-quarkus-console 
lrwxr-xr-x@ 1 cmoullia  staff  63 Feb  8 21:47 node_modules/@qshift/plugin-quarkus-console -> ../../../../../.config/yarn/link/@qshift/plugin-quarkus-console

TO UNLINK
yarn unlink "@qshift/plugin-quarkus-console"      

success Removed linked package "@qshift/plugin-quarkus-console".

Notes:

See: https://blog.alyssaholland.me/npm-yarn-link

cmoulliard commented 9 months ago

!! There is still a big big issue as we got this error when frontend is started yarn start

ERROR in ../../../backstage-quarkus-plugins/node_modules/@material-ui/core/esm/SwipeableDrawer/SwipeArea.js 4:0-31
Module not found: 
Error: Can't resolve 'react' in '/Users/cmoullia/code/openshift/backstage-quarkus-plugins/node_modules/@material-ui/core/esm/SwipeableDrawer'

The solution is maybe to perform a yarn install within the project where plugin has been linked - https://backstage.io/docs/local-dev/linking-local-packages/

cmoulliard commented 9 months ago

Temporary workaround is todo

From the github plugin project AND if we modify the module quarkus-console, execute the following commands to copy the file files generated to the backstage app project

PLUGIN_NAME=quarkus-console
BACKSTAGE_APP_PATH=$HOME/code/openshift/fork-backstage-playground

yarn tsc; yarn build

cp -rf plugins/$PLUGIN_NAME/dist/ $BACKSTAGE_APP_PATH/node_modules/@qshift/plugin-$PLUGIN_NAME/dist
cmoulliard commented 9 months ago

There is still an issue after we merged the PR #45 where react packages have been added as DevDependencies

Screenshot 2024-02-22 at 18 09 29

cmoulliard commented 8 months ago

Since we updated the plugins to use React 18 and playground has been bumped to 1.23.4, we got another error when we link the plugin within the playground backstage app => Cannot read properties of null (reading 'useContext')

Call Stack
 useContext
  backstage-quarkus-plugins/node_modules/react/cjs/react.development.js:1619:21
 useVersionedContext
  backstage-quarkus-plugins/node_modules/@backstage/version-bridge/dist/index.esm.js:59:59
 useEntity
  backstage-quarkus-plugins/node_modules/@backstage/plugin-catalog-react/dist/esm/useEntity-de64059a.esm.js:57:105
 useK8sObjectsResponse
  backstage-quarkus-plugins/plugins/quarkus-console/src/services/useK8sObjectsResponse.ts:21:90
 QuarkusApplicationInfo
  backstage-quarkus-plugins/plugins/quarkus-console/src/components/QuarkusApplicationInfo.tsx:39:123
 renderWithHooks
  node_modules/react-dom/cjs/react-dom.development.js:16305:18
 mountIndeterminateComponent
  node_modules/react-dom/cjs/react-dom.development.js:20069:13
 beginWork
  node_modules/react-dom/cjs/react-dom.development.js:21582:16
 HTMLUnknownElement.callCallback
  node_modules/react-dom/cjs/react-dom.development.js:4164:14
 Object.invokeGuardedCallbackDev
  node_modules/react-dom/cjs/react-dom.development.js:4213:16

Screenshot 2024-03-13 at 18 11 27

Idea ? @iocanel

cmoulliard commented 6 months ago

I did some researches and was able to find an approach which is working:

Instructions

// From plugins project
rm -rf node_modules
ln -s $HOME/PATH_TO_BACKSTAGE_PLAYGROUND/node_modules ./node_modules

// From $HOME/PATH_TO_BACKSTAGE_PLAYGROUND
rm -rf node_modules

# Edit the packages/app/package.json to add/replace the following dependencies
"@qshift/plugin-quarkus": "link:../../../BACKSTAGE_QUARKUS_PLUGINS/plugins/quarkus",
 "@qshift/plugin-quarkus-console": "link:../../../BACKSTAGE_QUARKUS_PLUGINS/plugins/quarkus-console",
"styled-components": "^6.1.8",

# Edit the packages/app/package.json to add/replace the following dependencies
"@qshift/plugin-quarkus-backend": "link:../../../fork-backstage-quarkus-plugins/plugins/quarkus-backend",

// Yarn install and launch
yarn install
yarn dev

Remark: A side affect of the approach followed here is that some plugin's dependencies should be declared part of the backstage project. Is there an alternative ?

WDYT ? @iocanel

cmoulliard commented 6 months ago

I tested another approach using the workspace option as documented here: https://dev.to/limal/simplify-your-monorepo-with-npm-7-workspaces-5gmj

I then created symbolic links to point to our plugins like this

ln -s /Users/cmoullia/code/openshift/fork-backstage-quarkus-plugins/plugins/quarkus ./plugins/quarkus
ln -s /Users/cmoullia/code/openshift/fork-backstage-quarkus-plugins/plugins/quarkus-console ./plugins/quarkus-console
ln -s /Users/cmoullia/code/openshift/fork-backstage-quarkus-plugins/plugins/quarkus-backend ./plugins/quarkus-backend

and changed too the links of the app and backend

# Edit the packages/app/package.json to add/replace the following dependencies
"@qshift/plugin-quarkus": "link:../../plugins/quarkus",
"@qshift/plugin-quarkus-console": "link:../../plugins/quarkus-console",

# Edit the packages/app/package.json to add/replace the following dependencies
"@qshift/plugin-quarkus-backend": "link:../../plugins//plugins/quarkus-backend",

Unfortunately, we get again the error Dispatcher is not null as the plugin quarkus (= > QuarkusVersionList) uses a Hook which is loaded using the lib packaged under the plugin node_modules folder

@iocanel

cmoulliard commented 6 months ago

I experimented the approach documented here : https://github.com/backstage/backstage/blob/master/docs/local-dev/linking-local-packages.md without success

I changed within the root package.json file and lerna.json file the workspaces to be used

  },
  "workspaces": {
    "packages": [
      "packages/*",
      "plugins/*",
      "../fork-backstage-quarkus-plugins/plugins/*"
    ]
  },

and

{
  "packages": [
    "packages/*",
    "plugins/*",
    "../fork-backstage-quarkus-plugins/plugins/*"
  ],
...

Executed again yarn install and yarn dev

We got when we click on http://localhost:3000/create the dispatch error BUT this time reported by the plugin-scaffolder-react/ and component: <ScaffolderPageContextMenu>

dispatcher is null
Call Stack
 useState
  node_modules/@backstage/plugin-scaffolder-react/node_modules/react/cjs/react.development.js:1623:3
 ScaffolderPageContextMenu
  node_modules/@backstage/plugin-scaffolder-react/dist/alpha.esm.js:1463:82
 renderWithHooks
  node_modules/react-dom/cjs/react-dom.development.js:16305:27
 mountIndeterminateComponent
  node_modules/react-dom/cjs/react-dom.development.js:20069:13
 beginWork
  node_modules/react-dom/cjs/react-dom.development.js:21582:16
 callCallback
  node_modules/react-dom/cjs/react-dom.development.js:4164:14
 invokeGuardedCallbackDev
  node_modules/react-dom/cjs/react-dom.development.js:4213:16
 invokeGuardedCallback
  node_modules/react-dom/cjs/react-dom.development.js:4277:31
 beginWork$1
  node_modules/react-dom/cjs/react-dom.development.js:27446:28
 performUnitOfWork
  node_modules/react-dom/cjs/react-dom.development.js:26552:12
cmoulliard commented 6 months ago

Forget my last remark. @iocanel

I created a new backstage app 1.27, set the workspaces as I commented before, declared our plugins to play with QuarkusVersionList - see screenshot and that works without having to create ln -s :-)

Note: We will have to bump our backstage-playground to see if the react errors are gone !

Screenshot 2024-05-17 at 16 06 42

Screenshot 2024-05-17 at 16 13 33

cmoulliard commented 6 months ago

The approach works too on our backstage-playground project and react error are gone at the condition that we also first remove the yarn.lock file

// bastage-playground/package.json
  "workspaces": {
    "packages": [
      "../fork-backstage-quarkus-plugins/plugins/*",
      "packages/*",
      "plugins/*"
    ]
  },
  ...
rm -rf node_modules
rm yarn.lock
yarn install; yarn dev
iocanel commented 6 months ago

I think that I prefer the simplicity of the mono repo, as it allows faster development.

If we continue with the split repo, even if we solve the problem of linking project, we still have drawbacks like:

cmoulliard commented 6 months ago

Conclusion about this ticket:

The only viable approach to have one node_modules folder is to declare part of the root/parent project the list of the workspaces to be handled by yarn and to define there the path to the plugins project.