VS2019 C# project always builds

Some of my projects always build, whereas others just say they’re already up-to-date.

I’ve finally got something that’s actually helping me isolate the problems, and it’s related to bringing in packages via NuGet that bring in lots of other dependencies.

The first step is to change the MSBuild output from Minimal to Diagnostics:

Run the build once just in case anything really has changed.

Build the project and look at the top of the diagnostics in the output window. As an example, I have this

1>Project 'AmberOptix.AOX3.Scripting' is not up to date. CopyLocal reference 'C:\dev\Projects\Amber\AOX3\AmberOptix.AOX3.Scripting\bin\Debug\System.Diagnostics.StackTrace.dll' is missing from output location.

Next I remove this assembly (System.Diagnostics.StackTrace.dll) from my packages.config file and the references. Then I build again and repeat the process until it eventually says that everything is up-to-date.

For some of the ‘missing’ assemblies I can guess that several others may also not be required. For example I deleted 3 System.Net.XXX packages and references when System.Net.Http was reported as missing.

As a guideline I had to remove over 20 packages and references from my scripting library to get this working.

An alternative to this manual approach of deleting one at a time is to delete all packages and references, then go through a cycle of adding back NuGet packages and normal assembly references as required.

I’m still sure there must be a better and safer way to do this! I think JetBrains’ ReSharper has tools but haven’t had a chance to trial yet.

macOS, Visual Studio Code, Python 3.7.5, OpenCV4

It took a few attempts to get a compatible Python and OpenCV library running under Visual Studio Code on macOS Catalina using a virtual environment. I made a video to show how I got this going – this post just adds some more details.

There is also an excellent tutorial from Microsoft:

Getting Started with Python in VS Code

Note: virtual machine rendering problem

Visual Studio Code running on a virtual machine may have problems rendering the interface. This seems to be related to the underlying Electron framework and GPU acceleration. I made a quick video to show how I got around this:

Fix rendering problems for Visual Studio Code running on a virtual machine

Install Python 3.7.5

A virgin Mac comes with Python 2.7 installed – this is not recommended and V3.7.5 works with OpenCV4 on a Mac. V3.8 does not work at the time of writing (although since I started writing this post it looks like it now does). Download the installer from the main python website by selecting Downloads, Mac OS X, and then selecting the 64-bit installer:

Run the installer – I used all default settings.

Install Visual Studio Code

Download the installer from Visual Studio Code and immediately move the downloaded file to the Applications folder. (This is the actual application, not an installer). Try to run once – macOS will refuse due to security:

Close the message, open System Preferences, and select the Security and Privacy settings. Then select “Open Anyway” to allow VSC.

Visual Studio Code should now start:

Configure Python

Open a folder by selecting Open folder and then add a new file. Save the file using the .py extension:

Visual Studio Code immediately offers to install the Python extension, select Install:

On a virgin Mac there will now be a prompt to install command line developer tools, so click Install if prompted and allow the installation to complete before returning to Visual Studio Code.

The status bar will show the selected interpret if everything has gone well:

Install the linter (pylint): this helps analyse the code for bugs and style issues. It also might not work first time but we can fix shortly…

If the terminal window suggests upgrading pip, the Python package manager, then go for it by running the following in the terminal window:

python3 -m pip install --upgrade pip

Make a virtual environment

A virtual environment is a self-contained directory tree that contains a Python installation for a particular version of Python

https://docs.python.org/3/tutorial/venv.html

Each project can use its own virtual environment to ensure any modules it requires don’t clash with modules in other projects.

From the terminal create a virtual environment:

python3 -m venv .venv

Visual Studio Code will detect this new environment and offer to select it for the current project folder – select Yes:

Because this is a new Python environment you may need to install the linter again:

Now – the bit that confused me… the project is now using the .venv virtual environment:

However, the terminal session has so far only created the environment, it has not activated it for itself. The shell identifier says:

jon@Jons-MacBook-Pro Python %

There are two ways to fix this. First, using the source command in the terminal window:

source .venv/bin/activate

Second, by creating a new Terminal session using the command palette. (Select View, ten Command Palette):

Now the terminal shows that it’s using the virtual environment:

Install OpenCV

At last we can install OpenCV. Using the terminal session in the virtual environment we can first search for OpenCV packages:

python3 -m pip search opencv  

When called with -m module-name, the given module is located on the Python module path and executed as a script

https://docs.python.org/3/using/cmdline.html

We see results like this:

opencv-utils (0.0.2) – OpenCV Utilities
ctypes-opencv (0.8.0) – ctypes-opencv – A Python wrapper for OpenCV using ctypes
opencv-wrapper (0.2.3) – A Python wrapper for OpenCV.
opencv-cython (0.4) – An alternative OpenCV wrapper
dajngo-opencv (0.3) – Django Opencv integratio
opencv-python (4.1.2.30) – Wrapper package for OpenCV python bindings

For this test I’m using opencv-python. The details on version 4.1.2.30 can be found on the Python Package Index site. Interestingly this version was only released a few hours ago and says it supports Python 3.8 😬 I guess I’ll try this on a virtual machine first to check it’s all ok!

Install OpenCV using pip:

python3 -m pip install opencv-python

Write some code and fix the linter

First test: import the OpenCV module and print the library version.

import cv2
print('Using OpenCV version {0}'.format(cv2.__version__))

After running this output is shown in the terminal:

But – there’s a problem. In the editor the linter is suggesting that cv2 is not a known module:

This has been seen before on the pylint GitHub issues page. For me, the solution is to edit the .vscode settings. Using ⇧⌘E (shift+command+E) to view the explorer page, expand the .vscode file and click settings.json:

Add a comma to the end of the line of the existing setting, then add the following new setting:

"python.linting.pylintArgs": ["--generate-members"]

My settings now look like this:

And now the red squiggle has gone from cv2.__version__ 😀

All that remains is to learn Python and OpenCV which will surely lead to great things!

Hope this helps.

3 useful keyboard mods for better editor support on Windows 10 using Parallels

Tested with Parallels Desktop 15 Pro Edition on macOS Catalina.

Problem 1: using Visual Studio or notepad++ or any similar multi-document application in Windows 10 on a Mac using Parallels, ⌘W (Command+W) maps to Alt+F4. This means To close just an editor page you have to revert to the original Windows shortcut of Control+F4 which is a minor pain on a Mac with the Touch Bar instead of function keys.

Solution 1: change the Parallels preferences to remap ⌘W to Ctrl+F4. ⌘Q will still close an application but ⌘W will now close an internal editor window; this is the same behaviour used in Safari to close the whole application (⌘Q) or close just one page (⌘W).

Problem 2: Control+Tab and Shift+Control+Tab don’t switch between editor windows.

Solution 2: a recent update to Parallels resulted in these shortcuts being used for Parallels tab switching. They don’t get passed on to the VM. Just unchecking these shortcuts fixes the problem.

Apple Trackpad on Windows with 3-finger drag!

A month ago, when I needed to get a new laptop for work, I switched from a MacBook Pro to a Dell XPS 9750, saving £100’s of pounds for what on paper is an almost identical laptop. Except it isn’t. Dell’s XPS is astonishingly good, and it’s a priviledge to own one, but I’ve become an Apple devotee over the years and can’t change it. My days are spent programming for Windows so I need a fast PC development environment and therefore the XPS makes sense. With the MBP I need to use VMWare Fusion or Parallels, and Apple are really pushing my (and other people’s) limits by charging so much money for memory and SSD upgrades. So I went for the XPS. 

Of all the little things I miss, 3 finger dragging is way up there. To be fair, the XPS’s trackpad is one of the best out there for Windows laptops, and with Windows 10 there is a double-tap to drag gesture which is fantastic. But the trackpad is too small and two taps (versus a single 3-finger drag) are one tap too many.

After a bit of Googling, I found Magic Utilities, a company that make drivers and utilities for Apple wireless keyboards, the magic mouse, and the Magic Trackpad. When I used Windows as a virtual machine on the MBP I got used to a particular mapping of the Alt and Cmd keys; the keyboard utility app lets me restore these mapping. Also, I’ve just discovered that the Eject key now becomes delete, although my fingers have 5 years of muscle memory which means that delete = Function + Backspace.

(Now, with the Apple keyboard, the function and control keys are where they should be IMO! There are loads of people asking Dell whether it’s possible to swap these keys on the Dell keyboards but it really can’t be done.)

Apple’s wireless Magic Trackpad performs terribly on Windows 10 by default. There are few (if any) gestures, and the whole feel of the cursor on the screen is terrible. Magic Utilities Trackpad app changes this completely:

Now I can scroll, drag, single-finder tap, and not a mechanical click in sight. 

I’ve now bought a one year licence after trialling this for a couple of weeks without any problems. Now I work with the XPS lid closed and just the wireless mouse and keyboard in front of an external 4K monitor. 

For the odd occasional where I might work 12 hours a day the little things, both positive and negative, soon accumulate, so the ~£20 cost of this bundle is really a bargain. 

Now if I could just find a similar utility to turn Windows 10 into Mac OS… 😀

Associate VS2017 with JSON files

Double-clicking a JSON file to try and open it in Visual Studio 2017 Professional doesn’t work; VS2015 worked fine. This link explains how to fix for the Community edition. The same principle applies for VS2017.

Using regedt32 first find the Visual Studio magic number:

Key: Computer\HKEY_CLASSES_ROOT\.json\OpenWithProgids
Value: VisualStudio.json.a8eb385c

2017-10-19 08_32_54-Registry Editor

 

Then find the associated shell key and create a new sub-key ‘Command’ with the path to devenv.exe as the default value:

Key: Computer\HKEY_CLASSES_ROOT\VisualStudio.json.a8eb385c\shell\Open\Command
Value: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"

2017-10-19 08_34_27-Registry Editor

 

Right-clicking a JSON file and selected Open with now looks like this:

2017-10-19 08_37_22-Sphere

 

VS2015 Update 2 VC.DB git mayhem

So today, after updating Visual Studio 2015 to Update 2, I was committing some changes to a project. Nestled amongst my own changes, deleted files, and new files, was a new VS2015 file, AOX3.VC.db (AOX3 is the name of my solution). This file is the new database engine and replaces (I believe) the SDF file. In Update 1 this was an experimental feature, but as of update 2 it’s official:

The new SQLite-based database engine is now being used by default.

I inadvertently added the file to my local git repository and then committed the changes.(I did the same thing on a couple of other projects, and made several more modifications and commits afterward).

The problem is, this file shouldn’t be in git, especially because it is around 150MB !

I only realised I had a problem when I tried to sync to my online (remote) repository, hosted by VSTS – luckily the attempt failed. The Synchronization panel had the following error message:

Failed to push to the remote repository. See the Output window for more details.

The output window had the following details:

Error encountered while pushing to the remote repository: Error while copying content to a stream.

Inner Exception:

Unable to read data from the transport connection: The connection was closed.

After a bit of googling I found I had to resort to the nuclear option to fix the problem.. deleting the file from the local git commit history.

Here’s what I did:

1. In VS2015 find the first commit that included the file. To do this I went to the Sync panel on the Team Explorer page and found the outgoing commit, then double-clicked it to see the commit details:

 

Screen Shot 2016-03-30 at 20.25.24.png

Screen Shot 2016-03-30 at 20.27.43.png

2. Note the parent commit ID of the first commit that included the new file (shown above, 645faa8c), then close VS2015.

3. Run a git bash shell in the solution folder. Then use the filter-branch command to delete the file from the last two commits. This required specifying a range of commits from the parent of the first commit all the way up the HEAD (the latest commit). This means noting the parent commit ID of the first commit that included the new file – as shown in the above screenshot. Here’s the git session:

jon@DESKTOP-0J37S2C MINGW64 /c/dev/Projects/Amber/AOX3 (dev_jon)
$ git filter-branch --tree-filter 'rm -f AOX3.VC.db' 645faa8c..HEAD
Rewrite 2fa87507296744092c2ac8b0c300b8c5973276ef (1/2) (0 seconds passed, remain
Rewrite 2917e9beea5ec0f546538345e33bd01586d359fb (2/2) (15 seconds passed, remaining 0 predicted)
Ref 'refs/heads/dev_jon' was rewritten

4. Open VS2015 and check the commits.. now the added/modified database file has disappeared from the list of changes:

Screen Shot 2016-03-30 at 20.35.52.png

5. I then edited my .gitignore file and added *.VC.db, then committed this mod and finally checked everything synchronized.. all worked as expected !

 

I guess the moral of the story is: don’t be flippant when adding new files, especially those you don’t recognize as your own. As the filter-branch documentation states:

This occurs fairly commonly. Someone accidentally commits a huge binary file with a thoughtless git add ., and you want to remove it everywhere.

This thoughtless programmer signing off, time to sleep 😉

 

 

JSON editing in Visual Studio 2013 – tip!

Just installed VS2013 on a clean Windows 10 virtual machine, noticed I wasn’t getting the same JSON editing experience I had on my previous VS2013 installation. The colour coding and auto-completion features were missing.

Solved the problem by modifying the installation and including the Microsoft Web Developer Tools:

W10_Dev

Now the editor uses colour coding, auto-completes the various brackets, and highlights errors.

Standing desk (ikea hack)

I’m starting my third week of standing, rather than sitting, at work. There’s been much written on this subject and I’m not 100% sure that it’s the right long term solution but so far I feel more focussed and less like a 12-hour-a-day slob.

My office already had some Ikea Expedit shelves on the wall, and I’d tried converting my table into a simple standing desk. But the table was around 6 inches too low and due to it having just four very extended legs it was also quite wobbly.

2013-12-03 09.15.08

Then I saw this excellent Ikea Hack for an Expedit Standing Desk.

My hack is a variation on the above desk system; I strengthened the front 5*1 unit by retaining the first and last internal panels – this had the advantage of providing a bit more storage as well. The wall shelves came down and formed sides to the desk, and now it’s starting to feel pretty cool (this being day 2 of this configuration).

I modelled the system on Google Sketchup:

i1 i2 i3 i4

Then I built it, and here it is:

2013-12-16 18.37.43 2013-12-16 18.38.12

Once I can lose the bits and bobs on the floor and tidy up the shelves it’s going to be super cool.