How to reference project in a different folder in ASP.net vNext - asp.net-core

Let's say I have a project (project1) that lives in [solution folder]\project1\. There's no problem adding a new project (project2) in [solution folder]\project2\ and referencing it as a dependency in project1's project.json with the line below:
"project2": ""
However what if I move project2 to a different (file system instead of solution) folder such as [solution folder]\lib\project2\? How do I add the reference in this case?
====================================
Just want to share a new tip:
If you have a project in a subfolder e.g. [solution folder]\lib\project1\ and want to reference another project located in the solution folder e.g. [solution folder]\project2\, make the following change to the global.json:
{
"sources": [""]
}

Add a global.json file to the solution folder with the following text in it:
{
"sources": ["lib"]
}
THT,
Bart

Related

PhpStorm : Use files from one project in other projects without copying them?

I have one project which contains some PHP services that I use as a template in all my other projects.
Is it possible to add these files to a new project without actually copying them to the new project's directory? Or, they could be copied but I want changes to one of the copies to affect the template.
For example. Template project contains
folder service with files "1.php", "2.php", "3.php"
and folder util with "u1.php" and "u2.php".
I want to use these files in one or more new projects but without copying them.
They should merge with the files of the new project and be treated as if they are in the new project's directory.
If the new project also has a folder "service" with files "4.php" and "5.php" the files 1 2 and 3 from the template should show there too.
And if I were to edit service/1.php in the new project, the original file in the template project would get changed as well.
Is something like this possible?

How to copy the content of a relative folder to the output folder on build

I have ASP.NET Core web project, which is located in:
~\Solutions\src\Web\
I have a second folder, which contains external assemblies:
~\dependencies\third_party\
So the dependencies and Solutions folders are sibling folders. I want to copy all dlls from the third_party folder to the output folder of the web project. In my case the output folder is:
~\Solutions\src\Web\bin\Debug\net461\win7-x64
I tried using the copyToOutput option in project.json:
"buildOptions": {,
"copyToOutput": {
"include": [ "../../dependencies/third_party/*" ]
}
}
The problem is that this will copy the assemblies in a child folder of the output folder. In my case it copies the assemblies in:
~\Solutions\src\Web\bin\Debug\net461\win7-x64\dependencies\third_party\{dlls are here}
Any ideas how to copy the assemblies at the root level of the output folder, not in any sub folders ?

Absolute module path resolution in TypeScript files in Visual Studio Code

I can't seem to convince Visual Studio Code to resolve absolute TypeScript module paths. Relative paths work, but absolute don't. I would like Visual Studio Code to resolve module paths from ./src folder on.
// This works when source file is in /src/here/there/file.ts
// and importing an interface in /src/api/interfaces.ts
import { Interface } from '../../api/interfaces';
// This doesn't work
import { Interface } from 'api/interfaces';
import { Interface } from '/api/interfaces';
import { Interface } from 'src/api/interfaces';
import { Interface } from '/src/api/interfaces';
// This works, but it is of course not supposed to be used
import { Interface } from 'c:/..../src/api/interfaces';
The last one of course doesn't count as each developer's project path is highly likely different. But even if we'd all set a system variable %ProjectXRoot% we can't use this variable in the code. Visual Studio Code will not resolve such module path. I've tried.
// Won't work
import { Interface } from '%ProjectXRoot%/api/interfaces';
Currently installed versions
• TypeScript: 1.8.10
• VSCode: 1.1.1
Question
I've tried to somehow configure Visual Studio Code to resolve absolute module paths, but I can't seem to do so. I've tried configuring tsconfig.json (in the project root) by adding baseUrl in two different places.
{
...
"compilerOptions": {
"baseUrl": "./src", // Doesn't work
...
},
"baseUrl": "./src", // Doesn't work either
...
}
I've tried values like src, ./src and ./src/, but none of them work in any of the upper configuration places.
So how does one configure Visual Studio Code to resolve absolute module paths from a certain folder?
If that's not possible, it would be at least better to resolve absolute paths from project root. How does Visual Studio Code determine that? Is it where you Open Folder or is it where the .vscode folder is?
But it would still be a viable solution for absolute paths. I've tried using ~ similar to Visual Studio, but to no avail either.
(Unresolved)
--
As steinso points out in his answer, all modules starting with /, ./ or ../ are considered relative. Especially the first one surprised me completely as I usually consider that a project root-relative path.
But this fact basically means that the main question now becomes: How can I provide module imports as absolute paths (from some project root folder path) at all? Starting paths with slashes usually meant absolute, but in this case it doesn't.
Even when I set compiler option moduleResolution to classic (so module resolution wont be looking into node_modules folder) the second set of imports above should actually all work as per Microsoft's linked document. But for some reason I still get red squiggly lines in Visual Studio Code and errors during compilation.
So how can I import a specific project module without providing an exact relative path to it, but rather just its own project-relative path?
To be able to use absolute paths from import in TypeScript using Visual Studio Code you should be using next version of TypeScript - typescript#next which is TypeScript v2. For that do the following:
Install typescript#next via npm. For installing TypeScript v2.x
npm i typescript#next -D
In Visual Studio Code
i) Go to menu File → Preferences → Workspace Settings (This generates the .vscode directory at the project root and initializes the settings.json file.)
ii) Put the following key:value pair in settings.json file
"typescript.tsdk": "node_modules/typescript/lib"
In tsconfig.json add following key:value pair to 'compilerOptions'
{
"compilerOptions" : {
"baseUrl": "./",
"paths" : {
"src/*": ["./src/*"]
}
}
}
Reload Visual Studio Code
If you have the following directory structure:
+ node_modules
+ src
| + app
| | + shared
| | | -service.ts
| | -main.ts
+ typings
- tsconfig.json
- webpack.config.json
- package.json
- index.html
Then to import /src/app/shared/service.ts from main.ts you could now import {} from 'src/app/shared/service;
If you are using webpack and ts-loader for transpiling the .ts files, you should add following to the resolve section of webpack.config.js configuration file.
resolve: {
extensions: ['', '.js', '.ts'],
alias: {
"src": path.resolve('./src')
}
}
Please refer to this for absolute module resolution.
You need to specify:
"compilerOptions": {
"moduleResolution": "classic"
...
The base path will then default to the directory of your tsconfig.json, add rootDir in compilerOptions to change it. EDIT: This does not seem to have any effect.
This will allow imports such as:
import { Interface } from 'api/interfaces';
Note that any path starting with . or ../ or / is considered relative.
Edit: Module resolution
Be aware of how the modules are resolved. The module path is still some what relative to the current file.
Let's use an example to illustrate for the current scenario:
Let's say you import { Interface } from "api/interfaces", from source file /src/views/View.ts. Typescript would then look for the module in the following paths:
/src/views/api/interfaces.ts
/src/api/interfaces.ts
/api/interfaces.ts
Note: how this still makes it relative, imagine if you import {Home} from "Home/Home" when you are located in /src/views/View.ts. In this case it would work even if the path is /src/views/Home/Home.
These are the possible resolutions:
/src/views/Home/Home -> Note how this would work.
/src/Home/Home
/Home/Home
More information can be found here:
http://www.typescriptlang.org/docs/handbook/module-resolution.html
If you've set up paths correctly:
{
…
"compilerOptions": {
…
"baseUrl": ".",
"paths": {
"~/*": ["app/assets/javascript/*"],
"*": ["node_modules/*", "app/assets/javascript/packs/*"]
}
}
}
And if you're still having issues, maybe try reloading Visual Studio Code: Cmd/Ctrl + Shift + P and type reload window.
It just randomly stopped working for me and is reporting problems. I spent around 20 minutes online trying to figure out what changed, only to realize that Visual Studio Code can glitch on absolute path resolution.

Why organize projects inside a src folder?

First of all, I would like to make it clear that I know that the src folder is not required. Indeed, one can simply create manually one directory for the project, make one valid project.json and one Startup class and everything should work fine even without Visual Studio.
My point is the following: when we create a new project using ASP.NET 5 from Visual Studio 2015 it creates a solution and inside the solution's folder it creates one src folder. Within this folder all projects are created.
Now why would anyone want to make one src folder inside the solution folder? Why not putting the projects direct onto the solution folder? Is there any advange on the organization of the project to put the projects inside a src folder? Why VS does that now?
You usually have more files inside your project that are not source code related like:
README.md
CONTRIBUTING.md
.gitignore
LICENSE
Build scripts
Docs and tools folders, etc.
And a tons of others files that depend on your configuration. So, if you have a src folder, you don't need to mix your source code files with those.
because you may have your code in src folder (class libraries, etc), test in your test folder, documentation in a documetation folder
in global.json you specify which folders does roslyn pick for compiling.

How to add non-source folders to IntelliJ IDEA project

Recently set up a multi module project in IntelliJ with the following structure:
/module1
/module2
/web-module
/sql
/lib
/a few more folders
I set up module1+2 and web-module as modules in IntelliJ so those show up, but how do you make the sql and lib folder show up in the project panel? They should be included in VCS as well, but IntelliJ ignores them. How do you add folders outside modules to a project?
Screenshot of project and explorer view:
This is not a strict answer to the question, but it worked for me so I'm posting, perhaps someone will find this useful.
If you want to add an arbitrary folder to your project (even from some different location than your projects), just add it as a module. You needn't worry about the type so much e.g. I needed to add a folder with some SQL scripts, I added it as a Java module and it's nicely visible in IntelliJ even though it has no maven structure or Java sources.
This is how to do it:
File > Project Structure > Modules
Add > New Module > ... (e.g. Java Module)
In the new module settings mark the subfolders that you want to see as 'Sources'
Voilà! :)
This is something that I typically see when creating a project from existing modules. All the modules will show in the project but not the other project related directories. These directories might be, configuration files, environment scripts or bundles of SQL scripts that don't fit neatly into an Intellij module type.
To show the rest of the project source files and directories, I create a parent module from the project root.
File->Project Structure->Modules
Create a new module using the + sign. The new module could be any type (I use java).
On the Next screen set the Content root and Module file location to the Project's root folder.
Select Finish
All of your other modules should now be submodules of the root, and your other project files should now show up.
Add and remove content roots
To add a new content root:
Go to File | Project Structure, or press Ctrl+Shift+Alt+S.
Select Modules under the Project Settings section.
Select the necessary module, and then open the Sources tab in the right-hand part of the dialog.
Click Add Content Root.
Specify the folder that you want to add as a new content root, and click OK.
source: https://www.jetbrains.com/help/idea/creating-and-managing-modules.html
I used File -> New -> Module from Existing Sources...
Then I simply select the folder and add it.
In Project view mode all directories (except the ignored ones from the settings) should show up. Of course the base folder for your multi-project has to be the folder above module1.
EDIT:
Your project should look like this (project view tree):
MY_PROJECT_ROOT (~/the/folder/to/your/project)
|- /module1
|- /module2
|- /web-module
And in this case, you should definitely see the other folders. I got a sample project set up where this is working.
EDIT 2:
From your screenshot, I assume you are missing the root directory (the project root is not as you expected). I added another screenshot. There should be a single root folder for your 3 modules. This one is missing at your screenshot. You have 3 separate folders with no common root folder. On MacOs, the project root is displayed in the window title. In my case it points to ~/devel/sandbox.
I guess you should try to create a new project in for that trunk folder. From the scratch. Then add the existing modules and you should be fine?!